Home Linux How to Get the Length of a String in Python

How to Get the Length of a String in Python

by admin

To get the length of a string in python you can use the len() function. len() is a built-in function in python and you can use it to get the length of string, arrays, lists and so on. In this short tutorial there are some examples of how to use len() to get the length of a string.

len() takes a string as a parameter and returns an integer, which is the length of that string. For example len(“buildvirtual”) will result in 12 being returned:

len("buildvirtual")
12

As you can see, the output shows the length of the string, which is 12. Another way to do this is to use the len() function against a string stored in a variable. For example:

a = "buildvirtual"
print(len(a))

Simple Program to Show the Length of a String

We can take this a step further by taking the above example and expanding it into a simple program to display the length of a string:

a = 'buildvirtual'
length = len(a)
print('Length of the string is:', length)

Summary

Really quick article, but you should now know how to use the len() function to get the length of a string in Python.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More