Created
June 26, 2017 06:32
-
-
Save lhsfcboy/756520dd5acf8b1ccc0af54be964631c to your computer and use it in GitHub Desktop.
Python中字符串相关的处理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = 'Life is short, you need Python' | |
a.lower() # 'life is short, you need Python' | |
a.upper() # 'LIFE IS SHORT, YOU NEED PYTHON' | |
a.count('i') # 2 | |
a.find('e') # 从左向右查找'e',3 | |
a.rfind('need') # 从右向左查找'need',19 | |
a.replace('you', 'I') # 'Life is short, I need Python' | |
tokens = a.split() # ['Life', 'is', 'short,', 'you', 'need', 'Python'] | |
b = ' '.join(tokens) # 用指定分隔符按顺序把字符串列表组合成新字符串 | |
c = a + '\n' # 加了换行符,注意+用法是字符串作为序列的用法 | |
c.rstrip() # 右侧去除换行符 | |
[x for x in a] # 遍历每个字符并生成由所有字符按顺序构成的列表 | |
'Python' in a # True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment