search

How to find the length of a string in Python?

To find the length of a string in Python, you can use the built-in function len(). Here's an example:

my_string = "Hello, World!"
length = len(my_string)
print(length)
# Output: 13

In this example, the len() function is used to return the number of characters in the string my_string. The resulting value is then stored in the variable length and printed.

The len() function takes a string as an argument and returns an integer representing the length of the string, which is the count of characters in the string.

Note that the length of a string includes all characters, including whitespace and special characters. So, in the example above, the length of the string "Hello, World!" is 13, including the space and exclamation mark.

Related Questions You Might Be Interested