This article is part of in the series

Python's len() method can be used to easily find the length of a string. It's a simple and quick way to measure the length of a string (the number of characters) without having to write a lot of code. The syntax for using the len() method is fairly straightforward, and hard to mess up -- even beginner Python coders should be able to handle it.  To see how it works, check out the following snippet:

str = "hey, I'm a string, and I have a lot of characters...cool!"
print = "String length:", len(str)

So to find the length of the string "str", you need to pass it through the len() method. Your answer will be the number of characters in the string, and that includes punctuation and spaces. The number of characters in the string "str" is 57, including the spaces, commas, periods, apostrophes, and exclamation points. So the output of the above code would be as follows:

String length: 57

The len() method will also count numbers along with spaces and punctuation if they're included in a string, like in the example below:

str = "I'm 26 years old."
print = "String length:", len(str)

The output for the above code, in which the str string has 17 characters, including spaces, apostrophes, numbers, and a period, would be as follows:

String length: 17

The len() method doesn't take any parameters other than the name of the string, so that's the only thing that can be passed through it. It isn't a particularly versatile method, but it does come in handy for this one specific purpose, and there are a number of different reasons you may come across in your coding adventures where you'll need to find the length of a string. You can also use this method in if statement to execute certain code based on the number of characters a string has (or doesn't have).