欢迎访问宙启技术站
智能推送

Python中如何使用len函数获取字符串长度

发布时间:2023-05-20 01:39:42

在Python中,可以使用内置函数len()获取字符串的长度。要使用len()函数获取字符串长度,只需将待计算长度的字符串作为参数传递到函数中。

例如,以下是一个简单的示例:

my_string = "Hello, World!"
string_length = len(my_string)

print("The length of my_string is", string_length)

输出:

The length of my_string is 13

在上面的示例中,我们将字符串"Hello, World!"赋值给变量my_string,并使用len()函数获取该字符串的长度。然后,我们将字符串长度赋值给变量string_length,并使用print()函数将结果输出。

总之,使用len()函数来获取Python字符串长度非常简单,只需调用它并将字符串作为参数传递即可。