Python get string length
Python get string length tutorial
To get the length of a string in Python , you can use the len() function.
Detailed explanation of Python len() function
definition
We pass the length of the string to be obtained into the len() function to obtain the length of the string.
grammar
len(string)
parameter
parameter | describe |
---|---|
string | The string to get the length of. |
return value
Returns the length of a string of type int .
the case
get string length
Use the len() function to get the length of a string
print("嗨客网(www.haicoder.net)")
# 使用 len() 函数,获取字符串的长度
str1 = "Hello,HaiCoder"
print('len =', len(str1))
After the program runs, the console output is as follows:
First, we define a string variable and assign it as "Hello, HaiCoder," and then we use the len() function to obtain the length of the string, and use the print () function to print the length of the string.
Get the length of the Chinese character string
Use the len() function to get the length of the Chinese character string
print("嗨客网(www.haicoder.net)")
# 使用 len() 函数,获取中文字符串的长度
str1 = "Hello,嗨客网(HaiCoder)"
print('len =', len(str1))
After the program runs, the console output is as follows:
First, we define a string variable containing Chinese and assign it as "Hello, HaiCoder". Then we use the len() function to get the length of the string, and use the print() function to print the The length of the string, 19 is output, that is, we found that a Chinese counts a length.
Python gets the string length summary
To get the length of a string in Python, you can use the len() function. Python len() function syntax:
len(string)