Python字符串长度:如何在Python中查找字符串长度

192 阅读3分钟

How To Find String Length In Python | Python String Length

Python中的字符串是一个不可改变的Unicode代码点序列。Pythonlen()返回字符串的长度。

Python 字符串的长度

要在Python找到一个字符串长度,可以使用**len()**函数。len()是一个内置的Python方法,返回字符串的长度。Python len()方法可以用来获取一个给定的字符串、序列和集合的长度。

语法

len(string)

参数

它需要一个字符串作为参数。

请看下面的代码。

# app.py

ST = "Millie Bobby Brown"
print("The String length of Millie Bobby Brown is:",len(ST))

BB = "Brayan Cranston"
print("The String length of Brayan Cranston is:", len(BB))

请看输出。

➜  pyt python3 app.py
The String length of Millie Bobby Brown is: 18
The String length of Brayan Cranston is: 15
➜  pyt

使用for循环和in运算符查找字符串的长度

我们将使用Pythonfor 循环in操作符来查找长度。

# app.py

def length(str):
    counter = 0
    for i in str:
        counter += 1
    return counter


str = "Millie Bobby Brown"
print(length(str))

请看输出。

➜  pyt python3 app.py
18
➜  pyt

使用while循环和切分查找字符串的长度

我们对一个字符串进行切分 ,使其在每次迭代中缩短一个,最终将导致一个空字符串。

这就是while循环停止的时候。保持对迭代次数的计算,就能得到字符串的长度。

# app.py

def length(str):
    counter = 0
    while str[counter:]:
        counter += 1
    return counter


str = "Millie Bobby Brown"
print(length(str))

请看输出结果。

➜  pyt python3 app.py
18
➜  pyt

使用 join 和 count 查找字符串长度

Python join()方法接收一个迭代器并返回字符串,它是迭代器中的字符串的连接。

各项之间的分隔符是调用该方法的原始字符串。

使用join并计算原始字符串中的连接字符串,也会得出字符串的长度。

请看下面的代码,以加深理解。

def length(str):
    if not str:
        return 0
    else:
        some_random_str = 'Leonard Nimoy'
        return ((some_random_str).join(str)).count(some_random_str) + 1


str = "Millie Bobby Brown"
print(length(str))

请看输出结果。

➜  pyt python3 app.py
18
➜  pyt

如何在Python中获得以字节为单位的字符串的大小

如果你想得到以字节为单位的字符串大小,你需要sys.getsizeof() 方法。

# app.py

import sys

str = "Millie Bobby Brown"
print("The byte size of Str is: ", sys.getsizeof(str))

请看输出结果。

➜  pyt python3 app.py
The byte size of Str is:  67
➜  pyt

让用户输入字符串的长度

下面这个Python的小程序使用input函数接受用户的输入。

使用 len() 函数显示所输入的字符串的长度。

# app.py

data = input('Enter a string? ')

print ("The length of entered string = ", len(data))

请看输出。

➜  pyt python3 app.py
Enter a string? KRUNAL
The length of entered string =  6
➜  pyt

这在你的程序需要用户输入并且对输入的字符数有一些限制时可能很有用。

如何在 Python 中找到列表的长度

len() 方法接受一个参数,你可以提供一个列表,并返回给定列表的长度。请看下面的代码。

# app.py

data = ['Millie', 'Finn', 'Noah', 'Gaten', 'Caleb', 'Sadie']

print ("The length of entered list = ", len(data))

请看输出结果。

➜  pyt python3 app.py
The length of entered list =  6
➜  pyt

如何找到字典的长度

要找到 Dictionary 的长度,使用 len() 方法。

在下面的代码中,我们定义了一个Dictionary。

# app.py

data = {'eleven': 'Millie', 'mike': 'Finn', 'will': 'Noah'}

print ("The length of entered dict = ", len(data))

请看输出结果。

➜  pyt python3 app.py
The length of entered dict =  3
➜  pyt

如何在Python中找到一个集合的长度

要找到集合的长度,使用 len() 方法。

在下面的代码中,我们已经定义了一个集合。

# app.py

data = {'eleven', 'Millie', 'mike', 'Finn', 'will', 'Noah'}

print ("The length of entered set = ", len(data))

请看输出结果。

# app.py

➜  pyt python3 app.py
The length of entered set =  6
➜  pyt

本教程就到此为止。

相关帖子

Python元组的长度

Python 列表的长度

Python字典的长度

Python集的长度

The postPython string length: How to find string length in Pythonappeared first onAppDividend.