字符串操作
1.转义字符
"转义字符"让你输入一些字符,他们用其他方式是不可能放在字符串里的。
spam = ‘say hi to Bob’s mother’ 这里字符串中的单引号会报错,在其中添加转义字符即可
spam = 'say hi to Bob\'s mother'
print ( spam)
一些转义字符
’ – 单引号
" – 双引号
\t – 制表符
\n – 换行符
2. 用三重引号的多行字符串
若要显示多行,当然可以通过添加多个换行符实现。不过使用多行字符串更容易
多行字符串的起止是三个单引号或三个双引号。三重引号之间的制表符、换行符依然有效,引号无须转义
print ( '''Dear Alice,
Eve's cat has been arrested for catnaping, cat burglary, and extortion.\n
sincerely,
Bob
''' )
""" 多行字符串通常用作多行注释 """
3. 字符串下标和切片
字符串和列表一样,可以通过下标访问。也可以作切片操作。这里省略不写
in 和 not in 操作符在字符串中依然有效
4.字符串方法:upper()字符串整体大写,lower()字符串整体小写
isupper()判断字符串是否大写,islower()判断字符串是否小写。首先字符串中至少有一个字母,且字符串全部都是大写或者小写,这两个方法才会返回True或者False
5. isX 字符串方法aaa
istitle() 返回True,如果字符串仅包含以大写字母开头、后面都是小写字母的单词
isalpha() 返回True,如果字符串只包含字母,并且非空
isalnum() 返回True,如果字符串只包含数字和字母,并且非空
isdecimal() 返回True,如果字符串只包含数字字符,并且非空
isspace() 返回True,如果字符串只包含空格、制表符、换行符,并且非空
print ( 'Hello' . istitle( ) )
print ( 'Hello world' . istitle( ) )
print ( 'Hello World' . istitle( ) )
print ( 'HelloWorld' . istitle( ) )
print ( 'Hello world' . isalpha( ) )
print ( 'Hello1 world' . isalnum( ) )
print ( '123d' . isdecimal( ) )
print ( '123d ' . isdecimal( ) )
print ( '123' . isdecimal( ) )
print ( '123 2' . isdecimal( ) )
print ( '1232' . isdecimal( ) )
6. 字符串方法 startswith()和 endswith()
print ( 'agg' . startswith( 'a' ) )
print ( 'agg' . endswith( 'g' ) )
print ( 'abc1' . startswith( 'ac' ) )
print ( 'ops' . endswith( '89' ) )
7. 字符串方法join()和split()
join()方法在一个字符串上调用,参数是一个字符串列表
返回的字符串以原字符串为分隔,将字符串列表的各个字符串连接起来
list1 = [ 'ab' , 'bc' , 'cd' ]
str2 = 'ji'
print ( str2. join( list1) )
str3 = 'ABC'
print ( str3. join( list1) )
split()方法与join()做的事正好相反
split()方法针对一个字符串调用,返回一个字符串列表
str4 = 'abABCbcABCcd'
print ( str4. split( 'ABC' ) )
str5 = 'ab bc cd'
print ( str5. split( ) )
str6 = '''Dear Alice,
How have you been? I am fine.
There is a container in the fridge
that is labeled "Milk Experiment".
Please do not drink it.
Sincerely,
Bob
'''
print ( str6. split( '\n' ) )
8. 用rjust()、ljust() 和 center()方法对齐文本
rjust()、ljust() 字符串方法返回调用它们的字符串的填充版本,通过插入空格来对其文本
这两个方法的第一个参数是一个整数长度,第二个参数是填充字符。
将这个字符串和填充字符按照某种对齐方式组成某个长度的新字符串
str7 = 'abc'
print ( str7. rjust( 10 ) )
str8 = str7. rjust( 10 )
print ( len ( str8) )
str9 = str7. ljust( 10 )
print ( str9)
print ( len ( str9) )
center()方法与ljust()、rjust()类似,它让文本居中,而不是左对齐,或右对齐
str10 = str7. center( 10 )
print ( str10)
def printPicnic ( dic, leftwidth, rightwidth) :
print ( 'picnic items' . center( rightwidth+ leftwidth) )
for i, j in dic. items( ) :
print ( i. ljust( leftwidth, '.' ) + str ( j) . rjust( rightwidth, ',' ) )
picnic = { 'apple' : 8 ,
'banana' : 5 ,
'pear' : 1 ,
}
printPicnic( picnic, 8 , 6 )
9. strip()、rstrip()和lstrip()没有传参,默认删除空白字符
strip() 删除开头末尾的空格,字符串中间的空格不会被删除
lstrip() 删除左边的空格
rstrip() 删除右边的空格
传了参数,就删字符串中的。如果参数是一个字符串,则删除字符串中参数的排列。
如果参数是abc,那么不管左右出现的是abc,acb,bca,bac,cab,cba全部删除
str11 = 'abc89767868cba'
print ( str11. lstrip( 'acb' ) )
print ( str11. rstrip( 'acb' ) )
print ( str11. strip( 'abc' ) )
str12 = '\nabc89767868cba\n'
print ( str12. lstrip( '\n' ) )
print ( str12. rstrip( '\n' ) )
print ( str12. strip( '\n' ) )