How to get the length of a string in python stack overflow

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

scssCopy codemy_string = "Hello, world!"
string_length = len(my_string)
print(string_length)

This will output the length of the string, which is 13. You can replace “Hello, world!” with any string of your choice.

Alternatively, you can also use string slicing to get the length of a string. Here’s an example:

scssCopy codemy_string = "Hello, world!"
string_length = my_string[-1] - my_string[0] + 1
print(string_length)

This will also output the length of the string, which is 13. In this example, we’re using the index of the first and last characters of the string to calculate the length.

Leave a Comment

Your email address will not be published. Required fields are marked *