How to calculate the length of a string in Python?

To get the length of a string in Python we can use the len() function.
The len() function accepts only one parameter and that is a string.
It returns an integer with the number of characters in the string.

Example of how to calculate the length of a string
The following snippet shows a basic example of how you can use this function to calculate the length of a string.

book = "The Snowball: Warren Buffett and the Business of Life"
len(book)

Output:

53

So with the len function you get the length of the string. If you want to do more calculations with the result, see our article about basic math in Python.

advertisement
advertisement