Definition of String Length Python
Python has a unique immutable data type known as a string which contains alpha-numeric values usually wrapped inside the quotes, and the string length is a function that helps in identifying the number of characteristics in python from numbers, letters, special characters to even spaces in between would be counted as a character or a value inside the string and length function is used in counting the total values inside the string. The len() function gives us the total length or number of characters that were present inside the given string.
Syntax:
The python length function’s basic structure is a character ‘len’ followed by the string or a variable with string data type inside a round bracket.
len(string)
Examples of String Length Python
Following are the examples are given below:
Example #1
x = 'EDUCBA'
y = 'Its a Beautiful Day'
z= '__Hello__'
print(len(x))
print(len(y))
print(len(z))
Output:
The total characters in the variable ‘x’ have six letters, ‘y’ has a total of 19 characters including the spaces, and ‘z’ has 9 including the underscore special characters, so the length function in python includes alpha-numeric characters, spaces, and also special characters.
We can use the length function in python to any lists, arrays, dictionary, tuple, etc. It has varied use since it’s a builtin function in python. The length function plays a role in optimising the code and how to efficiently use the characters stored inside the variable or an object.
Example #2
# len of a statement
statm = "Dostoevsky is a legendary writer"
print("The length of the statement is :", len(statm))
Output:
We have declared a simple statement as a string format to an object statm and
checked the length of the whole statement using the len() function.
Example #3
We can check the list’s total length and each value or string inside the list using the len() function.
### Length of a List #####
list1 = ["Horse","Shoe","Turtle"]
print("The length of the list is", len(list1))
print("The length of the 1st element is", len(list1[0]))
Output:
In the first output, we have printed the length of the whole list1, which contains three string values inside it, and the output is printed as 3. In the 2nd output, we have printed the length of the 1st element inside the list1, which is “Horse” it contains 5 letters, and hence we got the output printed as 5.
4.8 (14,175 ratings)
View Course
Example #4
Similar to the previous example we where have discussed the length function in a list, here we have declared the tuple, which is another data structure in python where we have declared string values inside a tuple and printed the length of the whole tuple and also the length of the 2nd value inside the tuple.
### Length of a Tuple #####
tup1 = ("Horse","Shoe","Turtle","Purple")
print("The length of the tuple is", len(tup1))
print("The length of the 1st element is", len(tup1[1]))
Output:
Example #5
In this example, we have declared a python dictionary inside a variable ‘Dict1’ where we have declared persons and their age.
### Length of a Dictionary #####
Dict1 = {'Sam': 21,'Jacob':19,'Alex':26,'Rob':33}
print("The length of the Dictionary is", len(Dict1))
Output:
Our dictionary Dict1 has 4 key-value pairs, and hence we have the output printed as 4. Python dictionaries cannot be sliced through indexing since dictionaries are unordered, unlike lists, Tuples, etc.
Example #6
In this example, we have discussed finding the length in an array data structure in python.
### Length of an Array #####
array1 = ['Sam','Jacob','Alex','Rob']
print("The length of the Array is", len(array1))
print("The length of the Array is", len(array1[3]))
Output:
An array data type is similar to a list in python, which is in an orderly manner. Here we have declared array1 with four person’s names and printed the total length of the array and the length of the third element “Rob” in the array, and hence we have the output as 4 & 3.
Example #7
Let’s discuss some more examples with codes on finding the length of the string in python.
str1 = 'google.com'
print("\n String str1 = ", str1)
print("Length of a String str1 = ", len(str1))
str2 = 'Pleasant Day'
print("\n String str2 = ", str2)
print("Length of a String str2 = ", len(str2))
str3 = '600118'
print("\n String str3 = ", str3)
print("Length of a String str3 = ", len(str3))
Output:
In this example, we have declared three-string with both numerical and word format inside the quotes, and hence python string doesn’t consider the difference between the value format, and it has printed the output of the 3 variables str1, str2 & str3. For better understanding, we have printed the actual string value and the length of the particular string.
Example #8
In this example, we have got the input string from the user and printed the length of that given string.
str1 = input(" Please Enter the string : ")
print("\n Actual String str1 = ", str1)
print("The length of a string given = ", len(str1))
Output:
In this example, we have got the statement from the user as input in a string format inside the str1 variable using the input() function in python. We have written the code to print the input statement along with the length of the statement where the statement we gave has 42 characters, and depending upon the input, our length of the string varies. This method can be very useful in optimizing our code by checking the length and value of the strings we declare.
Conclusion
In this article, we have discussed python string length in detail using various examples. We have also discussed how to find the length of the string in different data structures in python like Lists, Tuples, Dictionaries & Arrays. We have also checked the length by slicing the data structures and print the output. The python string length function is considered to be very important in our optimization of the code since it is used in various real-time scenarios. When working with different strings in a project, python’s length function will provide a useful hand.
Recommended Articles
This is a guide to String Length Python. Here we also discuss the introduction and syntax of string length python along with different examples and its code implementation. You may also have a look at the following articles to learn more –