How to use the len() function to get the length of a string in Python 3.x

WBOY
Release: 2023-07-29 17:01:22
Original
1406 people have browsed it

How to use the len() function to get the length of a string in Python 3.x

In Python, the len() function is a very commonly used function for getting the length of a string. This article will introduce how to use the len() function to get the length of a string in Python 3.x version, and provide some sample code.

The len() function is a built-in function in Python, which is used to return the length of an object or the number of elements. In the case of a string, the len() function will return the number of characters in the string.

The following is a simple sample code that shows how to use the len() function to get the length of a string:

str = "Hello, World!" length = len(str) print("字符串的长度为:", length)
Copy after login

Run the above code, the output will be:

字符串的长度为: 13
Copy after login

In the above example, we defined a string variablestrwith the content "Hello, World!". Then we use the len() function to get the length of the string and assign the result to the variablelength. Finally, we use the print() function to print out the length of the string.

In addition to strings, the len() function can also be used for other objects. For example, we can use it to get the number of elements in container objects such as lists, tuples, and dictionaries. Here is a sample code that shows how to use the len() function to get the length of a list:

list = [1, 2, 3, 4, 5] length = len(list) print("列表的长度为:", length)
Copy after login

Run the above code, the output will be:

列表的长度为: 5
Copy after login

In the above example, we define A list variablelistis created, containing 5 elements. Then we use the len() function to get the length of the list and assign the result to the variablelength. Finally, we use the print() function to print out the length of the list.

To summarize, the len() function is one of the most practical functions in Python and can be used to obtain the length of strings and other container objects. When writing a program, if you need to obtain the length of a string or the number of elements of other objects, you can use the len() function to achieve this. I hope this article can help you use the len() function to get the length of a string in Python.

The above is the detailed content of How to use the len() function to get the length of a string in Python 3.x. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!