str字符串:
一:基本使用
1、用途: 描述性质的数据,比如人的名字,单个爱好,地址
2、定义方式
name='egon' #name=str('egon')
1 x=str(1) 2 y=str(1.1) 3 z=str([1,2,3]) 4 n=str({'a':1}) 5 print(type(x)) 6 print(type(y)) 7 print(type(z)) 8 print(type(n))
3、常用操作+内置的方法
1 msg='hello world' 2 print(type(msg[5])) 3 print(msg[-1]) 4 msg[2]='A'
1 msg='hello world' 2 print(msg[1:5],type(msg[1:5])) 3 print(msg[6:-1]) 4 print(msg[6:11]) 5 print(msg[6:]) 6 print(msg[6::2]) 7 8 9 print(msg[0:]) 10 print(msg[::-1]) 11 msg='hello world' 12 print(msg[-3:-6:-1]) 13 print(msg[6:9:-1])#---这种取值方式会报错
1 msg='hello world' 2 print(len(msg))
1 print('SB' in 'my name is wcl,wcl is SB') 2 print('wcl' in 'my name is wcl,wcl is SB') 3 print('wc' not in 'my name is wcl,wcl is SB') # 推荐 4 print(not 'wc' in 'my name is wcl,wcl is SB')
1 name=' w cj ' 2 print(name.strip(' ')) 3 print(name.strip()) 4 name='****A*w*cj****' 5 print(name.strip('*')) 6 7 name='****zj****' 8 print(name.lstrip('*')) 9 print(name.rstrip('*')) 10 pwd=input('>>: ').strip() #pwd='123 ' 11 if pwd == '123': 12 print('login successful') 13 14 msg='cccabcdefgccccc' 15 print(msg.strip('c')) 16 17 print('*-=zj *&^'.strip('-= *&^'))
1 msg='zj:18:male:180:160' 2 l=msg.split(':') 3 print(l) 4 print(l[3])
1 msg='hello world' 2 for x in msg: 3 print(x)
其他操作:
1、strip,lstrip,rstrip
1 name='EoN' 2 print(name.lower()) 3 name='egonN' 4 print(name.upper())
1 print('wcl is SB'.startswith('wcl')) 2 print('wcl is SB'.endswith('B'))
1 print('my name is %s my age is %s' %('zj',18)) 2 print('my name is {name} my age is {age}'.format(age=18,name='zj')) # 可以打破位置的限制,但仍能指名道姓地为指定的参数传值 3 print('my name is {} my age is {}'.format('zj',18)) 4 print('my name is {0} my age is {1} {1} {1} {1}'.format('zj',18))
1 info='zj:18:male' 2 print(info.split(':',1)) 3 print(info.split(':',1)) #['zj','18:male'] 4 print(info.rsplit(':',1)) #['zj:18','male']
1 info='zj:18:male' 2 l=info.split(':') 3 print(l) 4 new_info='-'.join(l) 5 print(new_info) 6 7 num=['a','b','c'] 8 ':'.join(num) #'a'+':'+'b'+':'+'c' 9 10 num=[1,2,'c'] 11 ':'.join(num) #1+':'+2+':'+'c'
1 msg='my name is wcl,wcl is SB' 2 print(msg.replace('wcl','Pig',1)) 3 print(msg)
1 print('111.1'.isdigit())#False 2 print('1111'.isdigit())#True
1 案例 2 AGE=73 3 age=input('>>: ').strip() #age='asdfasdf' 4 if age.isdigit(): 5 age=int(age) 6 if age > AGE: 7 print('too big') 8 elif age < AGE: 9 print('too small') 10 else: 11 print('you got it') 12 else: 13 print('必须输入数字啊')
了解操作:
1 msg='my name is wlc,wlc is hahaha' 2 print(msg.find('wcl')) 3 print(msg.find('SB')) #找不到会返回-1 4 5 print(msg.index('wlc')) 6 print(msg.index('SB')) # 找不到index会报错 7 8 print(msg.find('wlc',0,3)) 9 10 print(msg.count('wcl')) 11 print(msg.count('wcl',0,15))#返回 'wcl'在start和end之间 在 msg出现的次数
1 name='zj' 2 print(name.center(30,'-'))#字符串的总长度为30,如果name没有30,则用'-',左右填满 3 print(name.ljust(30,'*'))#name在最左边,其余用'*'填满 4 print(name.rjust(30,'*'))#name在最后边,其他用'*'填满 5 print(name.zfill(30))#返回指定长度的字符串,原字符串右对齐,前面填充0。
1 name='zj\thello' 2 print(name)#zj hello 3 print(name.expandtabs(1))#zj hello
1 name='zj say hi' 2 print(name.capitalize())#Zj say hi 3 print(name.swapcase())#ZJ SAY HI 4 print(name.title())#Zj Say Hi
1 num1=b'4' #bytes 2 num2=u'4'#unicode,python3中无需加u就是unicode 3 num3='四'#中文数字 4 num4='Ⅳ'#罗马数字 5 print(type(num1))#<class 'bytes'> 6 print(type(num2))#<class 'str'> 7 print(type(num3))#<class 'str'> 8 print(type(num4))#<class 'str'> 9 print(type(num1),' ',type(num2),' ',type(num3),' ',type(num4)) 10 isdigt:bytes,unicode---是否是数字 11 print(num1.isdigit()) #True 12 print(num2.isdigit()) #True 13 print(num3.isdigit()) #False 14 print(num4.isdigit()) #False 15 16 isdecimal:uncicode---是否是小数 17 bytes类型无isdecimal方法 18 print(num2.isdecimal()) #True 19 print(num3.isdecimal()) #False 20 print(num4.isdecimal()) #False 21 22 isnumberic:unicode,中文数字,罗马数字 23 bytes类型无isnumberic方法 24 print(num2.isnumeric()) #True 25 print(num3.isnumeric()) #True 26 print(num4.isnumeric()) #True 27 28 三者不能判断浮点数 29 num5='4.3' 30 print(num5.isdigit())#False 31 print(num5.isdecimal())#False 32 print(num5.isnumeric())#False 33 ''' 34 总结: 35 最常用的是isdigit,可以判断bytes和unicode类型,这也是最常见的数字应用场景 36 如果要判断中文数字或罗马数字,则需要用到isnumeric 37 '''
1 name='zj123' 2 print(name.isalnum()) #字符串由字母或数字组成 3 print(name.isalpha()) #字符串只由字母组成 4 5 print(name.isidentifier())#判断字符串是否可为合法的标识符---变量的命名规则 6 print(name.islower())#检测字符串是否由小写字母组成 7 print(name.isupper())#检测字符串中所有的字母是否都为大写 8 print(name.isspace())#检测字符串是否只由空格组成 9 print(name.istitle())#检测字符串中所有的单词拼写首字母是否为大写
二:该类型总结
1 存一个值or存多个值
只能存一个值
2 有序or无序
有序
3 可变or不可变
不可变