The length of a string is the count of the number of characters present in that string, including spaces and punctuation marks. There are many ways to find python string length.
What is the Length of the String?
The length of a string can also be defined as the number of elements in the sequence of characters that make up the string. This string can include alphabets, digits, special characters, white spaces, and punctuation marks. Let’s understand the length of the string using a few examples.
Example:
st = "PrepBytes"
length = 9
In this example, the number of characters present in the string st is 9.
st = "PrepBytes@_#"
length = 12
In this example, the number of characters present in the string st is 9, including special characters.
st = "PrepBytes "
length = 12
In this example, the number of characters present in the string st is 9, including 3 white spaces.
Python Program to Find the Length of the String
Now, we will see different approaches to finding the python string length.
Approach 1: Using in-built function len()
In this approach, we have used the in-built python function len() to find the length of the string.
# Python string program st = "PrepBytes" print("Length of string '",st,"' is:", len(st))
Output:
Length of string ' PrepBytes ' is: 9
Approach 2: Using for loop and in operator
In this approach, the loop iterates over each character in the string and increments a counter for each character. The value of the counter is returned as a result, which is the length of the string.
# Python string length def length_of_string(st): count = 0 for char in st: count += 1 return count st = "PrepBytes" print("Length of string '",st,"' is:", length_of_string(st))
Output:
Length of string ' PrepBytes ' is: 9
Approach 3: Using recursion
In this approach, we will use the recursive function length_of_string, which calls itself until it finds the empty string. After completion of all the function calls in the end we get the length of the string.
# Python string length def length_of_string(st): if st == "": return 0 else: return 1 + length_of_string(st[1:]) st = "PrepBytes" print("Length of string '",st,"' is:", length_of_string(st))
Output:
Length of string ' PrepBytes ' is: 9
Approach 4: Using in-built function sum()
In this approach, we have used the in-built python function sum(), which will add 1 as many times as the characters present in the string. In the end, we will get the output sum as the length of the string.
# Python string length def length_of_string(st): return sum(1 for char in st) st = "PrepBytes" print("Length of string '",st,"' is:", length_of_string(st))
Output:
Length of string ' PrepBytes ' is: 9
Approach 5: Using enumerate function
In this approach, the for loop iterates over the characters in the string using the enumerate() function. The enumerate() function returns a tuple containing the index and value of each character in the string. The length of the string is determined by incrementing the value of length for each iteration of the loop, using index + 1. After all the characters have been processed, the final value of length is returned as the length of the string.
# Python string length def length_of_string(st): length = 0 for index, char in enumerate(st): length = index + 1 return length st = "PrepBytes" print("Length of string '",st,"' is:", length_of_string(st))
Output:
Length of string ' PrepBytes ' is: 9
Approach 6: Using reduce the () function of functools module.
In this approach, the reduce() function from the functools module is used to find the length of the string. The reduce() function applies a function (in this case, a lambda function) to the elements of a sequence (in this case, the string) in a cumulative way, reducing the sequence to a single value. The lambda function takes two arguments, acc and , where acc is the accumulator and is a placeholder for the current character in the string. For each iteration of the reduce() function, the value of acc is incremented by 1. The third argument to reduce() is the initial value of the accumulator, which is set to 0. After all the characters have been processed, the final value of acc is returned as the length of the string.
# Python string length from functools import reduce def length_of_string(st): return reduce(lambda acc, _: acc + 1, st, 0) st = "PrepBytes" print("Length of string '",st,"' is:", length_of_string(st))
Output:
Length of string ' PrepBytes ' is: 9
Conclusion:
In conclusion, this article will help you to understand what is the length of the string. In addition, you will also learn several python string length programs to understand how to find the length of the string using python language.
FAQs Related to String Length
1. Is there a difference in finding the length of a string in Python 2 and Python 3?
No, there is no difference in finding the length of a string in Python 2 and Python 3. The methods for finding the length of a string, such as using the len() function, are the same in both versions of Python.
2. Can I find the length of a string in Python even if it contains non-ASCII characters?
Yes, you can find the length of a string in Python even if it contains non-ASCII characters. The len() function and other methods for finding the length of a string, such as using a for loop, will work with strings containing non-ASCII characters, as long as the string is encoded correctly.
3. Can I find the length of a string in Python using a while loop?
Yes, you can find the length of a string in Python using a while loop. Here’s an example:
# python string length
def length_of_string(st):
length = 0
for i in st:
length += 1
return length
st = "PrepBytes"
print("Length of string:", length_of_string(st))
4. Can I find the length of a string in Python if it is a numeric string?
Yes, you can find the length of a numeric string in Python using the len() function or other methods described above. The len() function and other methods will work with numeric strings as long as the string is encoded correctly.
5. Can I find the length of a string in Python if it is a list of characters?
Yes, you can find the length of a list of characters in Python using the len() function. To find the length of a list of characters, simply pass the list as an argument to the len() function:
# python string length
li= ['P', 'r', 'e', 'p', 'B', 'y', 't', 'e', 's']
print(len(li))
6. Can I find the length of a string in Python if it is a Unicode string?
Yes, you can find the length of a Unicode string in Python using the len() function or other methods described above. The len() function and other methods will work with Unicode strings as long as the string is encoded correctly.
7. Is there a way to find the length of a string in Python if it contains multibyte characters?
Yes, you can find the length of a string in Python that contains multibyte characters using the len() function. The len() function in Python is capable of handling multibyte characters and will correctly count the number of characters in a string, even if some of them are multibyte characters.