Home > Article > Backend Development > Python's len() function: Get the length of a string
Python's len() function: Get the length of a string, specific code examples are required
Python is a simple, easy-to-learn and powerful programming language. Many convenient functions and methods are provided for string operations. Among them, the len() function is a commonly used function used to obtain the length of a string. In this article, we will explore the usage of the len() function and provide some concrete code examples.
First, let us look at the basic usage of the len() function. The len() function accepts a string as a parameter and returns the length of the string. The following is a simple example:
str1 = "Hello, world!" print(len(str1))
In this example, we define a string variable str1, which contains the string "Hello, world!". Then, we use the len() function to calculate the length of this string and print the result. Run this code and you will see the output is 13, which is the length of the string.
In addition, the len() function can be applied not only to ordinary strings, but also to other data types with similar behavior, such as lists, tuples, and dictionaries. Here are a few examples:
list1 = [1, 2, 3, 4, 5] print(len(list1)) tuple1 = (1, 2, 3, 4, 5) print(len(tuple1)) dict1 = {'name': 'Tom', 'age': 20, 'city': 'New York'} print(len(dict1))
In these examples, we define a list list1, a tuple tuple1, and a dictionary dict1 respectively, and use the len() function to get their lengths. Running this code, you will see that the output results are 5, 5, and 3, which are the lengths of these data structures.
In addition to being used to obtain the length of strings and other data structures, the len() function can also be used for iterable objects such as iterators and generators. Here is an example of using the len() function to get the length of an iterator:
iterator = iter([1, 2, 3, 4, 5]) print(len(iterator))
In this example, we use the iter() function to convert the list [1, 2, 3, 4, 5] into an iterator the iterator and use the len() function to get the length of the iterator. Run this code and you will see that the output is 5, which is the length of the iterator.
It should be noted that the len() function can only be used to return the number of characters or elements. For complex data structures, it cannot return meaningful results. In addition, for some special character sets, the len() function may not correctly calculate the length of the string. This is because the len() function calculates the length based on the number of characters, not the number of bytes.
In this article, we explore the usage of the len() function in Python and provide some concrete code examples. The len() function is a very useful function that can help us quickly get the length of a string or other data structure. I hope that through this article, you can have a deeper understanding of the len() function and be able to use it flexibly in actual programming.
The above is the detailed content of Python's len() function: Get the length of a string. For more information, please follow other related articles on the PHP Chinese website!