1.字符串的写法
1 )用一对双引号包起来:"字符串内容"
2 )用一对单引号包起来:'字符串内容'
3)用三对单引号或双引号包起来:""" 字符串内容"""或者'''字符串内容'''
需要注意的是,第三种方法支持换行,也就是说你在里面敲换行会显示出来但是前两种方法不会显示
2.字符串的下标:字符的具体位置
如:str="abcdefg"则字符串的长度为7,abcdefg分别保存在str[0]-str[6]中
3.字符串的切片:对操作的对象截取一部分的操作,字符串、列表、元组都支持切片操作
语法:
序列[开始位置下标:结束位置下标:步长]
例如:name='abcdefg'
print(name[2:5:1]) # cde
print(name[2:5]) # cde
print(name[:5]) # abcde
需要注意的是:
结果不包含结束位置下标对应的数据,下标为正负整数都可以。
步长是选取间隔,正负整数均可,默认步长为1。
如果不写开始的位置则默认从0开始选取
如果不写结束的位置则默认选取到最后
如果不写开始和结束的位置则选取全部
如果步长为负数,表示按倒序选取
下标-1表示最后一个数据,以此类推
如果从-4到-1结束,则选取方向为从左到右,但是步长为-1的话就是从右到左选取,两个方向发生冲突,则不能选取出数据