Updated June 16, 2023
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. The length function in Python determines the number of characters in a string, including numbers, letters, special characters, and even spaces. It counts each character as a value within the string, and the length function is employed to calculate the total number of values within the string. The len() function gives us the total length or number of characters 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
Code:
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 for any lists, arrays, dictionaries, tuples, etc. It has varied use since it’s a built-in function in Python. The length function plays a role in optimizing the code and how efficiently using the characters stored inside the variable or an object.
Example #2
Code:
# 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.
Code:
### 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 displayed the length of the entire list1, which contained three string values, and the outcome is 3. In the second output, we printed the length of the first element in the list1, which is “Horse”; it comprises 5 characters, therefore the result was displayed as 5.
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 displayed string values inside a tuple and printed the length of the whole tuple and also the size of the 2nd value inside the tuple.
Code:
### 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.
Code:
### 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, so the output is 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.
Code:
### 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 for finding the string length in Python.
Code:
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 a 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 receive an input string from the user and then print the length of the provided string.
Code:
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, the 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. When working with different strings in a project, python’s length function will provide a useful hand.
Recommended Articles
We hope that this EDUCBA information on “String Length Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.