马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
第014讲 字符串:各种奇葩的内置方法
① str1 = 'I love FishC.com'
str1[2:6]
>>> str1[:7] + '插入字符串' +str1[7:]
'I love 插入字符串FishC.com'
>>> str1[:7] + '插入字符串' +str1[8:]
'I love 插入字符串ishC.com'
②扩展阅读:http://bbs.fishc.com/forum.php?mod=viewthread&tid=38992
capitalize:
>>> str2 = 'xiaoxie'
>>> str2.capitalize()
'Xiaoxie'
>>> str2 = 'AS A GIRL'
>>> str2.casefold()
'as a girl'
>>> str2.center(40)
' AS A GIRL '
>>> str4 = '小甲鱼'
>>> str4.islower()
False
>>> str5 = 'FishC'
>>> str5.istitle()
False
>>> str5 = 'Fishc'
>>> str5.istitle()
True
>>> str6 = ' i love you.'
>>> str6.lstrip()
'i love you.'
>>> str6 = 'i love you'
>>> str6.partition('ov')
('i l', 'ov', 'e you')
>>> str6.partition('lo')
('i ', 'lo', 've you')
>>> str6
'i love you'
>>> str6.replace('fishc','fishC')
'i love you'
>>> str6.replace('you','him')
'i love him'
>>> str6.split()
['i', 'love', 'you']
>>> str6.split('l')
['i ', 'ove you']
>>> str6.split('o')
['i l', 've y', 'u']
>>> str7 = ' sssaaasss '
>>> str7.strip()
'sssaaasss'
>>> str5.swapcase()
'fISHC'
>>> str7
' sssaaasss '
>>> str7.translate(str.maketrans('s','b'))
' bbbaaabbb '
>>> str.maketrans('s','b')
{115: 98}
|