Toggle navigation

Python 判断字符串长度

Document 对象参考手册 Python3 实例

给定一个字符串,然后判断该字符串的长度。

实例 1:使用内置方法 len()

str = "qianmu"
print(len(str))

执行以上代码输出结果为:

6

实例 2:使用循环计算

def findLen(str): 
    counter = 0
    while str[counter:]: 
        counter += 1
    return counter 
  
str = "qianmu"
print(findLen(str))

执行以上代码输出结果为:

6

Document 对象参考手册 Python3 实例