每天10分钟,带你一周精通Python:附录A处理字符串

每天10分钟,带你一周精通Python:附录A处理字符串

注意:

标记[start, [end]]表示的是开始和结束是可选择的参数。如果仅提供了一个数字作为参数,它会被认为是开始。

#标志着一个评论的开始。
’ ’ ’ 标志着一个多行评论的开始与结束。
真正的代码是以monotype字体表示的。
=>标志着输出的开始。

count(sub, [start,[end]])

返回子字符串sub在字符串中出现的次数。
这个函数是大小写敏感的。

[样例]

#在下面的例子中,“s”在索引3,6和10的位置出现
#计算整个字符串
This is a string'.count('s')
=>3
#计算从索引4到字符串的结尾
This is a string'.count('s',4)
=>2
#计算从索引4到10-1的位置
This is a string'.count('s', 4, 10)
=>1
计算“T”。只有一个“T”,因为函数是大小写敏感的。
'This is a string'.count('T')
=>1

endswith(suffix,[start,[end]])

如果字符串以指定的后缀suffix结尾,就返回True,否则返回False。
后缀suffix也可以是要寻找的多个后缀的元组。
这个函数是大小写敏感的。

[样例]

# “man” 这三个字符出现在索引4到6的位置
# 检查整个字符串
'Postman'.endswith('man')
=> True
# 从索引3的位置检查到字符串的末尾
Postman'.endswith('man',3)
=> True
# 检查索引2到6-1的位置
'Postman'.endswith('man',2,6)
=> False
# 检查索引2到7-1的位置
'Postman'.endswith('man',2,7)
=> True
# 使用一个后缀的元组(从索引2到6-1的位置检查)
'Postman'.endswith(('man','ma'),2,6)
=> True

find/index(sub,[start,[end]])

返回字符串中子字符串sub首次出现的索引位置。
find()在无法找到sub时返回-1。
index()在无法找到sub时返回ValueError。
这个函数是大小写敏感的。

[样例]

# 检查整个字符串
'This is a string'.find('s')
=>3
# 从索引4到字符串结尾开始检查字符串
'This is a string'.find('s', 4)
=>6
# 从索引7到11-1开始检查字符串
'This is a string'.find('s', 7,11)
=> 10
# 没有找到子字符串sub
'This is a string'.find('p')
=>-1

'This is a string'.index('p')
=> ValueError

isalnum()

如果字符串中的所有字符都是字母和数字,并最少存在一个字符,则返回True,否则返回False。
数字和字母不包括空格。

[样例]

'abcd1234'.isalnum()
=> True
'a b c d 1 2 3 4'.isalnum()
=> False
'abcd'.isalnum()
=> True
'1234'.isalnum()
=> True

isalpha()

如果字符串中的所有字符都是字母,并且至少有一个字符,则返回True,否则返回False。

[样例]

'abcd'.isalpha()
=> True
'abcd1234'.isalpha()
=> False
'1234'.isalpha()
=> False
'a b c'.isalpha()
=> False

isdigit()

如果字符串中的所有字符都是数字,并且至少有一个字符,则返回True,香则返回False。

[样例]

'1234'.isdigit()
=> True
'abcd1234'.isdigit()
=> False
'abcd'.isdigit()
=> False
'12 3 4'.isdigit()
=> False

islower()

如果字符串中的所有字符都是小写形式,并且至少有一个字符,则返回True,否则返回False。
[样例]
‘abcd’.islower()
=> True
‘Abcd’.islower()
=> False
‘ABCD’.islower()
=> False

isspace()

如果字符串中的字符只是空格,并且只有一个字符,则返回True,否则返回False。

[样例]

' '.isspace()
=> True
'a b'.isspace()
=> False

istitle()

如果字符串都是首字母大写的字符串,并且至少有一个字符,则返回True。

[样例]

'This Is A String'.istitle()
=> True
'This is a string'.istitle()
=> False

isupper()

如果字符串中的所有字符都是大写的,并且至少有一个字符,则返回True,否则返回False。

[样例]

'ABCD'.isupper()
=> True
'Abcd'.isupper()
=> False
'abcd'.isupper()
=> False

join()

使用分隔符号把提供的参数连接成一个字符串并返回。

[样例]

sep='-'
myTuple= ('a','b', 'c')
myList=['d', 'e',’f']
myString ="Hello World"
sep.join(myTuple)
=>'a-b-c'
sep.join(mytist)
=>'d-e-f'
sep.join(myString)
=>“H-e-l-l-0--W-0-r-l-d"

lower()

复制一份字符串,并把该字符串中的字符转换为小写形式。

[样例]

'Hello Python'.lower()
=> "hello python'

replace(old, new[, count])

复制一份字符串,并把该字符串中出现的所有old字符串替换为new字符串。
count参数是可选的。如果提供了该选项,则仅仅前面count次数出现的字符串会被替换。
这个函数是大小写敏感的。

[样例]

# 替换所有出现的字符串
'This is a string'.replace('s','p')
=> 'Thip ip a ptring'

# 替换前两次出现的字符串
'This is a string'.replace('s', 'p',2)
=> 'Thip ip a string'

split([sep [,maxsplit]])

使用sep作为界定字符串,把字符串分隔,返回一个单词的列表。
sep和maxsplit是可选项的。
如果sep没有指定,界定字符为空格。
如果指定maxsplit,最多会做maxsplit次分隔。
这个函数是大小写敏感的。

[样例]

'''
使用逗号作为界定分隔符来分隔字符串。
注意在输出的单词“is”,“a”和“string”前都有一个空格。
'''
'This, is, a, string'.split(',')
=> ["This', ' is', ' a', ' string']
# 使用空格作为界定分隔符来分隔字符串
'This is a string'.split()
=> ['This', 'is', 'a', 'string']
# 仅做2次分隔
'This, is, a, string'.split(',',2)
=> ['This', ' is', ' a, string']

splitlines([keepends])

返回字符串所有行的列表,以每行的边界符来分隔。
除非指定keepends并为真,否则返回的结果列表中并不会包括每行的分隔符。

[样例]

# 通过\n来分隔行 
'This is the first line.\nThis is the
second line'.splitlines()
=> ['This is the first line.','This is the second line.']
# 分隔多行字符串,即使用'''标记的字符串
'''This is the first line.
This is the second line.'''.splitlines()
=> ['This is the first line.','This is the second line.']
# 分隔并保留行的分隔符
'This is the first line.\nThis is the
second line.'.splitlines(True)
=> ['This is the first line.\n','This is the second line.']

'''This is the first line.
This is the second
line.'''.splitlines(True)
=> ['This is the first line.\n', 'This is the second line.']

startswith(prefix[,start[,end]])

如果字符串以前缀开始,则返回True,否则返回False。
前缀prefix也可以是要寻找的前缀的元组。
这个函数是大小写敏感的。

[样例]

# 'Post'在索引0到3的位置出现
# 检查整个字符串
'Postman'.startswith('Post')
=> True
# 检查字符串从索引位置3到结尾
'Postman'.startswith('Post',3)
=> False
# 检查字符串从索引位置2到6-1
'Postman'.startswith('Post',2,6)
=> False
# 检查字符串从索引位置2到6-1
'Postman'.startswith('stm', 2, 6)
=> True
# 使用索引的元组(检查字符串从索引位置3到结尾)
'Postman',startswith(('Post', 'tma'),3)
=> True

strip([chars])

复制一个字符串,返回该字符串首尾位置移除字符串char的结果。如果没有指定char,空格将会被移除。
这个函数是大小写敏感的。

[样例]

# 移除空格
'    This is a string     ' .strip()
=> 'This is a string'
# 移除“s”。因为“s”并不在字符串的首尾出现,所以没有移除任何字符
'This is a string'.strip('s')
=> 'This is a string'
# 移除’g’。
'This is a string'.strip('g')
=> This is a strin'

upper()

复制一个字符串,并返回该字符串的大写形式。

[样例]

'Hello Python'.upper()
=>"HELLO PYTHON'

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值