How to find the length of a string in Python using the `len()` function?

Published on Aug. 22, 2023, 12:17 p.m.

Yes, you can find the length of a string in Python using the len() function. The len() function returns the number of characters in the string.

Here’s an example:

my_string = "hello, world!"
string_length = len(my_string)
print(string_length)

Output:

13

In this example, we have defined a string, and then we call the len() function on the string to get the number of characters in the string.

Note that the len() function can also be used to get the length of other data types such as lists, tuples and dictionaries which as well return the number of elements in the container.

Tags: