The Python String len() method is used to retrieve the length of the string. A string is a collection of characters and the length of a string is the number of characters (Unicode values) in it.
The len() method determines how many characters are there in the string including punctuation, space, and all type of special characters. The number of elements which is stored in the object is never calculated, so this method helps in providing the number of elements.
For example, the string "Tutorials Point" has 15 characters in it including the space.
SyntaxFollowing is the syntax of Python String len() method:
len(str)Parameters
This method does not accept any parameter.
Return ValueThis method returns the length of the string.
ExampleIn the following example we are finding the length of the given string "this is string example....wow!!!" using Python String len() method:
str = "this is string example....wow!!!"; print "Length of the string: ", len(str)
Following is the output of the above code:
Length of the string: 32Example
Following is an example where a list of strings is created. Then using the len() method we return the length of the list:
# finding the length of the list li = ["Python","Java","CSS","Javascript"] # Returning the length of the list print("The length of the given list is:", len(li))
Output of the above code is as follows:
The length of the given list is: 4Example
In the example given below an array of elements is created. Then by using the len() method, the size of the array is returned:
# finding the length of the array array = ["Jyoti","Radhika","Kriti","Suhana","Akriti","Ankita","Nachiket"] # Returning the length of the list print("The length of the given array is:", len(array))
While executing the above code we get the following output:
The length of the given array is: 7Example
In the following example a dictionary is created and the length of the dictionary is returned:
# finding the length of the dictionary dictionary = {'Player': 'Sachin Tendulkar', 'Sports':'Cricket', 'age':48} res = len(dictionary) # Returning the length of the list print("The length of the given array is:", res)
When we run above program, it produces following result:
The length of the given array is: 3
python_strings.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4