两个值ID相同值一定相同,ID不同值可以相同。
定义:在单引号或双引号或三引号内,有一串字符组成
作用:名字,性别,国籍,地址等描述信息。
例如:name=‘egon‘
优先掌握的操作:
#1、按索引取值(正向取+反向取) :只能取 #2、切片(顾头不顾尾,步长) #3、长度len #4、成员运算in和not in #5、移除空白strip #6、切分split #7、循环
需要掌握的操作方法:
#1、strip(移除空白),lstrip,rstrip #2、lower,upper #3、startswith,endswith #4、format(按照顺序传值)的三种玩法 #5、split(切分),rsplit(反向切分) #6、join(按照某个特定提示符把某列表里的字符拼成字符串并且只能把列表里的元素都是字符串的情况下才能用join拼接) #7、replace #8、isdigit(常用与判断,只能判断bytes与unicode型数字)
例如:
#strip
name=‘*egon**‘
print(name.strip(‘*‘))
print(name.lstrip(‘*‘))
print(name.rstrip(‘*‘))
#lower,upper
name=‘egon‘
print(name.lower())
print(name.upper())
#startswith,endswith
name=‘alex_SB‘
print(name.endswith(‘SB‘))
print(name.startswith(‘alex‘))
#format的三种玩法
res=‘{} {} {}‘.format(‘egon‘,18,‘male‘)
res=‘{1} {0} {1}‘.format(‘egon‘,18,‘male‘)
res=‘{name} {age} {sex}‘.format(sex=‘male‘,name=‘egon‘,age=18)
#split
name=‘root:x:0:0::/root:/bin/bash‘
print(name.split(‘:‘)) #默认分隔符为空格
name=‘C:/a/b/c/d.txt‘ #只想拿到顶级目录
print(name.split(‘/‘,1))
name=‘a|b|c‘
print(name.rsplit(‘|‘,1)) #从右开始切分
#join
tag=‘ ‘
print(tag.join([‘egon‘,‘say‘,‘hello‘,‘world‘])) #可迭代对象必须都是字符串
#replace
name=‘alex say :i have one tesla,my name is alex‘
print(name.replace(‘alex‘,‘SB‘,1))
#isdigit:可以判断bytes和unicode类型,是最常用的用于于判断字符是否为"数字"的方法
age=input(‘>>: ‘)
print(age.isdigit())
示例
了解的操作方法:
#1、find,rfind,index,rindex,count #2、center,ljust,rjust,zfill #3、expandtabs #4、captalize,swapcase,title #5、is数字系列 #6、is其他