Python is a Scripting language; we can easily program in Python on Ubuntu System or any other Linux System and execute them without any extra effort. Sometimes we need to know the length of String which we are using in Python programs especially when String is provided by the user. So in this Article we will discuss the different ways to get the length of a String in Python on the Linux System.

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 “.pyextension 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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/1-22.jpg" data-lazy- height="214" src="data:image/svg xml,” width=”929″>

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:

print(n Find the length of  “ Linuxhint ” “)

String= “Linuxhint”

print(” The length of “Linuxhint” is: “,len(String))

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/2-22.jpg" data-lazy- height="289" src="data:image/svg xml,” width=”930″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/3-22.jpg" data-lazy- height="273" src="data:image/svg xml,” width=”930″>

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:

print(n Find the length of String “ This is Linuxhint ” “)

String= “This is Linuxhint”

print(” The length of String is: “,len(String))

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/4-20.jpg" data-lazy- height="674" src="data:image/svg xml,” width=”929″>

Now to see the output of above written code run the below mentioned command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/5-18.jpg" data-lazy- height="252" src="data:image/svg xml,” width=”930″>

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:

def get_length (String):

    count=0

    while String [count:]:

       count = 1

    return count

String= “Linuxhint”

print(n The length of String “Linuxhint” is: “,get_length(String))

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/6-16.jpg" data-lazy- height="677" src="data:image/svg xml,” width=”1043″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/7-11.jpg" data-lazy- height="260" src="data:image/svg xml,” width=”1045″>

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:

def get_length (String):

    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))

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/8-5.jpg" data-lazy- height="674" src="data:image/svg xml,” width=”1048″>

After the execution of above mentioned code using Python3, we get the below mentioned output:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/9-5.jpg" data-lazy- height="285" src="data:image/svg xml,” width=”1046″>

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.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/7c99efcc024949e17b176845c757687e?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Alishba Iftikhar

I am currently an undergraduate student in my 1st year. I am an internee author with Linuxhint and loved learning the art of technical content writing from senior authors. I am looking forward to opting my career as a full time Linux writer after I graduate.