pythonの文字列タイプ
25069 ワード
:
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))