Requirements
Any version of Python should be installed on your system(Python3 is preinstalled in latest Ubuntu versions)
Below I have discussed different methods to get the length of String in Python, you can follow any of the method you find easier or appropriate for yourself:
How to find Length of a String In Python Using len() Function
So the first method we are going to discuss is to find the length of String using len() function. The len() is the predefined function of Python which returns the length of a String, array, tuple or dictionary in a program. With the help len() function we will write a program in a Python file in Ubuntu(Linux OS) to get the length of String.
Syntax: The syntax of len function is mentioned below:
value: It can be a String, tuple, array or dictionary whose length we want to find.
Creating a Python file : Firstly generate a file with “.py” extension using nano editor (Python file); create “Python_file.py” file in current working directory by below mentioned command:
You can set the name of the file of your choice.
How to find the length of a single string using Python
Now to find the length of given string, write the below mentioned code in the newly created Python file:
String= "Linuxhint"
print(" The length of "Linuxhint" is: ",len(String))
The sign “#” is used to add comments; these are explanatory not executable statements.
Now execute above written code using Python, I am using Python3 in below mentioned command as it is preinstalled in Ubuntu:
How to find length of a group of strings using Python
To get the length of group of String, write the below mentioned code in to the “Python_file.py” file:
String= "This is Linuxhint"
print(" The length of String is: ",len(String))
Now to see the output of above written code run the below mentioned command:
How to find the length of a string using while Loop in Python
If you don’t want to use len() function or you want to calculate the length of String by procedure to verify the output of length Function then you can use while loop for this purpose, write the below mentioned code in to “Python_file.py” file to get length of String “Linuxhint” using while loop:
count=0
while String [count:]:
count += 1
return count
String= "Linuxhint"
print("\n The length of String "Linuxhint" is: ",get_length(String))
Code is explained using Comments in the above shown image. After execution of above mentioned code using Python3 you will get the below shown output:
Find the Length of String using “for Loop”
We can also check the length of given string by using “for loop” without using len() function, write the below mentioned command in the “Python_file.py” file to calculate length of String using for loop:
count=0
for j in String:
count += 1
return count
String= "Linuxhint"
Print("\n Length of String Usinf For Loop")
print("\n The length of String "Linuxhint" is: " ,get_length(String))
After the execution of above mentioned code using Python3, we get the below mentioned output:
You can calculate the length of any String using above mentioned Methods.
Conclusion
We can run Python programs on Ubuntu (Linux System) by creating a Python file and executing it with any version of Python installed. In this Article we have discussed different approaches with examples to get the length of String in Python like using len() function, it is the most easiest and efficient approach, and using ” for loop” and “while loop” we can also find the length of String. If you are a Python programmer and want to find the length of String in any of your Python programs on Ubuntu then this Article will surely help you.