How to print the length of a string in Python?

In Python, you can use the `len()` function to calculate the length of a string, which will return the number of characters in the string, including spaces and special characters.

For example:

Declare a string variable:

string = "Hello, World!"

2. Use the `len()` function to get the length of a string.

length = len(string)

Print the length of the string.

print("字符串的长度为:", length)

When running the above code, the output will be:

字符串的长度为: 13

In this example, the string “Hello, World!” contains 13 characters, so the return value of `len()` is 13. It’s important to note that if the string contains Unicode characters (such as Chinese characters), each Unicode character will be counted as one character.

It is important to note that the `len()` function can be used for strings, lists, tuples, and other types of iterable objects.

Leave a Reply 0

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


广告
Closing in 10 seconds
bannerAds