一:基本使用 1 用途: 描述性质的数据,比如人的名字,单个爱好,地址 2 定义方式 name='egon' #name=str('egon') x=str(1) y=str(1.1) z=str([1,2,3]) n=str({'a':1}) #str 用来转成字符串类型,可为任意类型 print(type(x)) print(type(y)) print(type(z)) print(type(n)) 3 常用操作+内置的方法 优先掌握的操作(*****): 1、按索引取值(正向取+反向取) :只能取 msg='hello world' print(type(msg[5])) #取出的z值为空格同样为str print(msg[-1]) msg[2]='A' #里面的信息不能改不能赋值 2、切片(顾头不顾尾,步长) msg='hello world' print(msg[1:5],type(msg[1:5])) print(msg[6:-1]) print(msg[6:11]) #输出的为worl 最后一位不取 print(msg[6:]) print(msg[6::2]) 了解(**) print(msg[0:5]) print(msg[0:-1]) print(msg[6:-1:2]) print(msg[-3:-6:-2]) print(msg[0:]) print(msg[::-1]) #为空格就是默认为从头开始,负1为步长为取得字符输出相反 msg='hello world' print(msg[-3:-6:-1]) print(msg[6:9:-1]) 3、长度len msg='hello world' print(len(msg)) #len为记录msg一共有11位 4、成员运算in和not in print('SB' in 'my name is alex,alex is SB') print('alex' in 'my name is alex,alex is SB') print('egon' not in 'my name is alex,alex is SB') # 推荐 egon这个值是否包含的对错判断 print(not 'egon' in 'my name is alex,alex is SB') print('n' in 'my name is alex,alec is sb') print('n' not in 'my name is sdsd,dffis ') 5、移除空白strip name=' e gon ' print(name.strip(' ')) #去除空格键的,左右都去除。但是字符中间的不能去除 print(name.strip()) name='****A*e*gon****' print(name.strip('*')) name='****egon****' print(name.strip('*')) print(name.lstrip('*')) #只去除左边的* print(name.rstrip('*')) #只去除右边的* pwd=input('>>: ').strip() #pwd='123 ' ,以防用户输错,系统崩溃 if pwd == '123': print('login successful') msg='cccabcdefgccccc' 'c' print(msg.strip('c')) print('*-=egon *&^'.strip('-= *&^')) #依次去除所包含的符号 print('-=egon*&'.strip('-=*&')) 6、切分split msg='egon:18:male:180:160' l=msg.split(':') #split是去除:符号然后用列表的方式呈现 print(l) print(l[3]) 7、循环 msg='hello world' for x in msg: #把msg中的字符一个一个的打印出来用for循环 print(x) 需要掌握的操作(****) 1、strip,lstrip,rstrip 2、lower,upper name='EoN' print(name.lower()) #把当中的大写换成小写 name='egonN' print(name.upper()) #把小写字母换成大写字母 3、startswith,endswith print('alex is SB'.startswith('alex')) # 判断是否为alex开头的字符 print('alex is SB'.endswith('B')) #判断是否B为结尾的字符 4、format的三种玩法 print('my name is %s my age is %s' %('egon',18)) print('my name is {name} my age is {age}'.format(age=18,name='egon')) # 可以打破位置的限制,但仍能指名道姓地为指定的参数传值 print('my name is {} my age is {}'.format('egon',18)) #按顺序赋予值 print('my name is {0} my age is {1} {1} {1} {1}'.format('egon',18)) #多次简单方便的赋予相关的值 5、split,rsplit info='egon:18:male' print(info.split(':')) # print(info.split(':',1)) #['egon','18:male']把第一个冒号用逗号代替形成新的列表 print(info.rsplit(':',1)) #['egon:18','male']把最后一个冒号用逗号代替 6、join:只能将元素全为字符串的列表拼成一个大的字符串 info='egon:18:male' l=info.split(':')#转换为列表 print(l) new_info='-'.join(l)#把列表的逗号换成-符号代替 print(new_info) num=['a','b','c'] print(':'.join(num)) #更换成'a:b:c' num=[1,2,'c'] ':'.join(num) #1+':'+2+':'+'c' 只有全是字符串才能由列表转换成一个大的字符串 7、replace msg='my name is wupeiqi,wupeiqi is SB' print(msg.replace('wupeiqi','Pig',1))#把字符串内的第一个wupeiqi改成pig, print(msg) 8、isdigit print('111.1'.isdigit()) #判断字符内是否为整型,False print('1111'.isdigit()) #这个是整型,所以为Ture AGE=73 age=input('>>: ').strip() #age='asdfasdf' 去除用户输入的空格 if age.isdigit(): #判断是否是数字 age=int(age)#转换成整型 if age > AGE: print('too big') elif age < AGE: print('too small') else: print('you got it') else: print('必须输入数字啊傻叉') 其他操作(了解即可)(**) 1、find,rfind,index,rindex,count msg='my name is alex,alex is hahaha' print(msg.find('alex')) print(msg.find('SB')) #找不到会返回-1 print(msg.index('alex')) print(msg.index('SB')) # 找不到index会报错 print(msg.find('alex',0,3)) print(msg.count('alex')) print(msg.count('alex',0,15)) 2、center,ljust,rjust,zfill print('info egon'.center(50,'-')) print('info egon'.ljust(50,'-')) print('info egon'.rjust(50,'-')) print('info egon'.zfill(50)) 3、expandtabs print('a\tb\tc'.expandtabs(1)) 4、captalize,swapcase,title print('my name is egon'.capitalize()) print('my Name Is egon'.swapcase()) print('my name is egon'.title()) 5、is数字系列 num1=b'4' #bytes num2=u'4' #unicode,python3中无需加u就是unicode num3='壹' #中文数字 num4='Ⅳ' #罗马数字 isdigit():bytes,unicode print(num1.isdigit()) print(num2.isdigit()) print(num3.isdigit()) print(num4.isdigit()) isdecimal():unicode print(num2.isdecimal()) print(num3.isdecimal()) print(num4.isdecimal()) isnumberic;unicode,中文,罗马 print(num2.isnumeric()) print(num3.isnumeric()) print(num4.isnumeric()) 6、is其他 print('abasdf123123'.isalnum()) print('asdfasdf'.isalpha()) print('egon'.islower()) print('ABC'.isupper()) print(' '.isspace()) print('My Name Is Egon'.istitle()) 二:该类型总结 1 存一个值or存多个值 只能存一个值 2 有序or无序 有序 3 可变or不可变 不可变 name='egon' print(id(name)) name='alex' print(id(name))
python的字符串类型
猜你喜欢
转载自blog.csdn.net/qq_35540539/article/details/80514814
今日推荐
周排行