欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python_公共方法

程序员文章站 2022-07-01 16:15:06
1.计算长度 2.索引取值 3.切片 ......

1.计算长度

value = "wangdianchao"
# 计算字符个数(长度)
number = len(value)
print(number)

2.索引取值

value = "wangdianchao"
# 获取value“0”位置的字符
number = value[0]
print(number)
value = "wangdianchao"
# 获取value右侧第一个的字符
number = value[-1]
print(number)

3.切片

value = "wangdianchao"
# 2<=索引位置<3
number = value[2:4]
print(number)

value = "wangdianchao"
# 从2取到最后
number = value[2:]
print(number)

value = "wangdianchao"
# 从0取到-2之前
number = value[:-2]
print(number)