In Python, different sequence types are used to store multiple input data values such as strings, lists, tuples, etc. The string data type stores a sequence of the alphabet, numbers, or characters. Python supports multiple built-in functions that perform operations on the strings. Some commonly used operations include removing a string, adding a new string, calculating the stringβs length, and so on.
This guide will elaborate on how to get string length in Python. The following aspects of string length will be discussed in this Python Guide:
- How to Get Python String Length?
- Example 1: Using βlen()β Function to Get Python String Length.
- Example 2: Getting Length of String Using for Loop.
- Example 3: Getting Length of Python String Using if-else Statement.
So let’s get started!
How to Get Python String Length?
To determine the length of a Python string, we can use an inbuilt function named “len()” or a user-defined function with “for loops” and “if-else” statements. The syntax of the len() function is shown below:
len(obj)
The len() function accepts an object as an argument and retrieves the number of items in that object. The object can be either a sequence or a collection. Passing a string value to the len() function will return the length/number of characters in that string.
The below examples are used to find Python string length:
Example 1: Using the βlen()β Function to Get Python String Length
The inbuilt Python function βlen()β gets the length of the string. This function returns the number of elements/characters of the passed string. Additionally, this function is used to get the length of a tuple, list, dictionary, and other data storage container.
Code:
value = "Python Guide!"
print(len(value))
In the above code, the string βPython Guideβ is initialized in a variable named βvalueβ. The βlen()β function takes the variable βvalueβ as an argument and shows the length of that string.
Output:
The output shows the length of a given string value.
Example 2: Getting Length of String Using for Loop
In the example code given below, the βfor loopβ is used inside the user-defined function to calculate the length of a string value.
Code:
def Length(str_value):
len = 0
for i in str_value:
len += 1
return len
str_value = "Python String Guide!"
print(Length(str_value))
In the above code:
- The function named βlengthβ is defined to take the value of the given string inside the parenthesis.
- The counter value β0β is initialized in the variable βlenβ. The zero value indicates that the length of the string begins from the first character position.
- The βforβ loop iterates on the string value, and on each character value, it will add β1β in the variable named βlenβ.
- The return statement returns the function result to a variable named βlenβ.
Output:
The output represents the string length named βstr_valueβ.
Example 3: Getting Length of Python String Using if-else Statement
In the following example, the length of the string is calculated using the βif-elseβ statement inside the user-defined function.
Code:
def Length(str_value):
if str_value == '':
return 0
else:
return 1 + Length(str_value[1:])
str_value = Length("Python and Linux Guide!")
print(str_value)
In the mentioned code:
- The function named βlengthβ is defined using the def keyword.
- The βif-elseβ statement is initialized inside the βLengthβ function to find the length of an input string.
- If the string value is empty, then the βifβ statement returns β0β.
- The βelseβ statement executes when the string value is not zero. The statement inside the βelseβ block uses the index method technique to find the length of the string.
- The statement β[1:]β indicates that the count of string value starts from the 1st character and counts the length until the last character.
Output:
The length is displayed in the output.
That’s all from this Python Guide!
Conclusion
In Python, the βlen()β method is used to get the length of the string. The βfor loopβ and βif-elseβ statement can be used with the aid of a user-defined function to get the stringβs length. The βlen()β function is the simplest way to calculate the stringβs length. It can also get the length of other data structures such as a dictionary, tuple, list, etc. This write-up guide presented a detailed overview of how to get the length of a string in Python.