一、 创建字符串
s1 = 'python'
s2 = "python"
二、字符串的操作
print ( 'python' * 3 )
pythonpythonpython
print ( 'python' [ 3 : ] )
hon
print ( 'py' in 'python' )
print ( 'aaa' in 'python' )
True
False
print ( 'python is a better ' )
print ( '%s is a netter' % 'python' )
python is a better
python is a netter
三、字符串的拼接
a = 'boy'
b = 'girl'
c = a + b
c
'boygirl'
'@@@@' . join( [ a, b] )
'boy@@@@girl'
四、内置方法
count() center() startswith() find() format() lower() upper() strip() replace() split()
str = 'hellow python'
str . count( 'l' )
2
str . capitalize( )
'Hellow python'
str . center( 50 , '*' )
'******************hellow python*******************'
str . endswith( 'y' )
False
str . startswith( 'he' )
True
str2 = 'he\tllow python'
print ( str . expandtabs( tabsize= 20 ) )
hellow python
str . find( 'p' )
7
str2 = 'hellow python {boy}'
str2. format ( boy = 'bob' )
'hellow python bob'
str . index( 'p' )
7
str3 = '123asf'
str3. isalnum( )
True
str3. isdecimal( )
False
str3. isdigit( )
False
str3. isnumeric( )
False
str3. isidentifier( )
False
str3. islower( )
True
str3. isupper( )
False
str . isspace( )
False
'My Python' . istitle( )
True
str3. lower( )
'123asf'
str3. upper( )
'123ASF'
'SFsdf' . swapcase( )
'sfSDF'
str3. ljust( 20 , '@' )
'123asf@@@@@@@@@@@@@@'
str3. rjust( 20 , '@' )
'@@@@@@@@@@@@@@123asf'
print ( ' pythton' . strip( ) )
pythton
str3. replace( '123' , '456' )
'456asf'
str3. rfind( 's' )
4
'12 345 343' . split( ' ' )
['12', '345', '343']
'congage agege' . title( )
'Congage Agege'