4、常见数据类型之str字符串操作

1、查看数据类型

 type(str)

a=123
print(a,type(a))
  123 <class 'int'>
其中123 为数据内容 class 中的int代表类型

2、字符串操作截取

a='helloworld'
print(a[0]) ##打印索引为0的,,所以内容为h
在这里,helloworld的索引,从左到右,依次为0-1-2-3-4-5-6-7-8-9

print(a[1:]) ##打印索引为1到后面的所有 内容为 elloworld
print(a[-1])  #打印倒数第一个的   内容为   d
print(a[1:3])  #打印内容索引为1 到 索引到2  ,内容为 el 
print(a[1:7:2])  这里的2为步长度  从索引为1的取到索引为6的,且隔2索引取1值,所以取到的索引为1  3  5  取值为 elw
print(a[1],a[3],a[5])   取值为 elw
注意》》范围取值的时候是顾头不顾尾的,所以索引为3的取不到的  

猜你喜欢

转载自www.cnblogs.com/wyf-577513827/p/9167036.html
今日推荐