您的位置:首页 > 编程语言 > Python开发

小甲鱼python零基础笔记014字符串:各种奇葩的内置方法

2019-02-09 20:14 387 查看

调用格式:str1 = '字符串'   str1.方法名()。

方法名 描述 举例
capitalize() 把字符串的第一个字符改为大写

>>> str1 = 'monkey'
>>>str1.capitalize() 
'Monkey'

casefold() 把整个字符串的所有字符改为小写 >>> str1 = 'MONKEY'
>>>str1.casefold() 
'monkey'
center(width) 将字符串居中,并使用空格填充至长度 width 的新字符串 >>> str1 = 'ju Zhong'
>>> str1.center(18)
'     ju Zhong     '
count(sub[, start[, end]]) 返回 sub 在字符串里边出现的次数,start 和 end 参数表示范围,可选。 >>> str1 = 'fswwff'
>>> str1.count('w')
2
encode(encoding='utf-8', errors='strict') 以 encoding 指定的编码格式对字符串进行编码。encoding –> 要使用的编码,如”UTF-8”。
errors –> 设置不同错误的处理方案。默认为 ‘strict’,意为编码错误引起一个UnicodeError。 其他可能得值有 ‘ignore’, ‘replace’, ‘xmlcharrefreplace’, ‘backslashreplace’ 以及通过 codecs.register_error() 注册的任何值。
>>> str1 = '编码'
>>> str_utf8 = str1.encode('UTF8')
>>> str_gbk = str1.encode('GBK')
>>> print(str1)
编码
>>> print(str_utf8)
b'\xe7\xbc\x96\xe7\xa0\x81'
>>> print(str_gbk)
b'\xb1\xe0\xc2\xeb'
>>> print(str_utf8.decode('utf8'))
编码
>>> print(str_gbk.decode('gbk'))
编码
endswith(sub[, start[, end]]) 检查字符串是否以 sub 子字符串结束,如果是返回 True,否则返回 False。start 和 end 参数表示范围,可选。 >>> str1 = '我爱你我'
>>> str1.endswith('我')
True
>>> str1.endswith('我',1,3)
False
>>> str1.endswith('我',1,4)
True
expandtabs([tabsize=8]) 把字符串中的 tab 符号(\t)转换为空格,如不指定参数,默认的空格数是 tabsize=8。 >>>str1 = 'I\tlove\tyou'
>>> str1.expandtabs()
'I       love    you'
find(sub[, start[, end]]) 检测 sub 是否包含在字符串中,如果有则返回索引值,否则返回 -1,start 和 end 参数表示范围,可选。 >>> str1 = '我爱您!亲爱的母亲!'
>>> str1.find('爱')
1
>>> str1.find('爱',5,7)
5
>>> str1.find('爱',4,5)
-1
index(sub[, start[, end]]) 跟 find 方法一样,不过如果 sub 不在 string 中会产生一个异常。

>>> str1 = '我爱您!亲爱的母亲!'

>>> str1.index('母亲')
7

isalnum() 如果字符串至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False。 >>> str1 = 'Iloveyou1314'
>>> str1.isalnum()
True
>>> str12 = 'I hate you!'
>>> str12.isalnum()
False
isalpha() 如果字符串至少有一个字符并且所有字符都是字母则返回 True,否则返回 False。 >>>str1 = 'Iloveyou'
>>> str1.isalpha()
True
isdecimal() 如果字符串只包含十进制数字则返回 True,否则返回 False。 >>> str1 = '123456'
>>> str1.isdecimal()
True
>>> str2 = '1.23456'
>>> str2.isdecimal()
False
isdigit() 如果字符串只包含数字则返回 True,否则返回 False。 >>> str1 = '89230'
>>> str1.isdigit()
True
islower() 如果字符串中至少包含一个区分大小写的字符,并且这些字符都是小写,则返回 True,否则返回 False。 >>> str1 = 'father'
>>> str1.islower()
True
isnumeric() 如果字符串中只包含数字字符,则返回 True,否则返回 False。 >>> str1 = '34'
>>> str1.isnumeric()
True
>>> str2 = '十六'
>>> str2.isnumeric()
True
isspace() 如果字符串中只包含空格,则返回 True,否则返回 False。 >>> str1 = '   空格    '
>>> str1.isspace()
False
istitle() 如果字符串是标题化(所有的单词都是以大写开始,其余字母均小写),则返回 True,否则返回 False。 >>> str1 = 'I Love Fishc.Com'
>>> str1.istitle()
True
isupper() 如果字符串中至少包含一个区分大小写的字符,并且这些字符都是大写,则返回 True,否则返回 False。 >>> str1 = '230K'
>>> str1.isupper()
True
join(sub) 以字符串作为分隔符,插入到 sub 中所有的字符之间。 >>> str1 = '1010'
>>> str1.join('大中华香烟')
'大1010中1010华1010香1010烟'
ljust(width) 返回一个左对齐的字符串,并使用空格填充至长度为 width 的新字符串。 >>> str1 = '左对齐左填空'
>>> str1.ljust(30)
'左对齐左填空                        '
lower() 转换字符串中所有大写字符为小写。 >>> str1 = 'I LOVE 小乌龟 Niko'
>>> str1.lower()
'i love 小乌龟 niko'
lstrip() 去掉字符串左边的所有空格 >>> str1 = '    去掉字符串左边空格'
>>> str1.lstrip()
'去掉字符串左边空格'
partition(sub) 找到子字符串 sub,把字符串分成一个 3 元组 (pre_sub, sub, fol_sub),如果字符串中不包含 sub 则返回 ('原字符串', '', '') >>> str1 = '桂林山水甲天下'
>>> str1.partition('山水')
('桂林', '山水', '甲天下')
replace(old, new[, count]) 把字符串中的 old 子字符串替换成 new 子字符串,如果 count 指定,则替换不超过 count 次。

>>> str1 = 'mamimamihong'
>>> str1.replace('m','M')
'MaMiMaMihong'

>>> str1.replace('m','M',2)
'MaMimamihong'

rfind(sub[, start[, end]]) 类似于 find() 方法,不过是从右边开始查找。 >>> str1.rfind('爱')
5
rindex(sub[, start[, end]]) 类似于 index() 方法,不过是从右边开始。 >>> str1.rindex('亲')
8
rjust(width) 返回一个右对齐的字符串,并使用空格填充至长度为 width 的新字符串。 >>> str1 = '右对齐左填空'
>>> str1.rjust(30)
'                        右对齐左填空'
rpartition(sub) 类似于 partition() 方法,不过是从右边开始查找。

>>> str1 = '桂林山水甲天下'

>>> str1.rpartition('山水')
('桂林', '山水', '甲天下')

rstrip() 删除字符串末尾的空格。 >>> str1 = '去掉字符串末尾空格       '
>>> str1.rstrip()
'去掉字符串末尾空格'
split(sep=None, maxsplit=-1) 不带参数默认是以空格为分隔符切片字符串,如果 maxsplit 参数有设置,则仅分隔 maxsplit 个子字符串,返回切片后的子字符串拼接的列表。

>>> str1 = 'I love fishc com'
>>> str1.split()
['I', 'love', 'fishc', 'com']

>>> str1.split('o',3)
['I l', 've fishc c', 'm']

splitlines(([keepends])) 在输出结果里是否去掉换行符,默认为 False,不包含换行符;如果为 True,则保留换行符。。

>>> str1 = 'I love this\nbut he does not like it\rif she want\nthat\rI will buy it for her\n'
>>> str1.splitlines()
['I love this', 'but he does not like it', 'if she want', 'that', 'I will buy it for her']

>>> str1.splitlines(True)
['I love this\n', 'but he does not like it\r', 'if she want\n', 'that\r', 'I will buy it for her\n']

startswith(prefix[, start[, end]]) 检查字符串是否以 prefix 开头,是则返回 True,否则返回 False。start 和 end 参数可以指定范围检查,可选。 >>> str1 = 'A big elephant!'
>>> str1.startswith('A')
True
>>> str1.startswith('e',6)
True
strip([chars]) 删除字符串前边和后边所有的空格,chars 参数可以定制删除的字符,可选。 >>> str1 = '     去掉 字符串 前后 所有 空格       '
>>> str1.strip()
'去掉 字符串 前后 所有 空格'
swapcase() 翻转字符串中的大小写。 >>> str1 = 'I LOVE 小乌龟 Niko'
>>> str1.swapcase()
'i love 小乌龟 nIKO'
title() 返回标题化(所有的单词都是以大写开始,其余字母均小写)的字符串。

>>> str1 = 'i love fishc.com'
>>> str1.title()

'I Love Fishc.Com'

translate(table) 根据 table 的规则(可以由 str.maketrans('a', 'b') 定制)转换字符串中的字符。 >>> str1 = '各个国家有各个国家的国歌'
>>> str1.translate(str.maketrans('国','國'))
'各个國家有各个國家的國歌'
upper() 转换字符串中的所有小写字符为大写。

>>> str1 = 'I LOVE 小乌龟 Niko'

>>> str1.upper()
'I LOVE 小乌龟 NIKO'

zfill(width) 返回长度为 width 的字符串,原字符串右对齐,前边用 0 填充。 >>> str1 = '右对齐前填0'
>>> str1.zfill(30)
'000000000000000000000000右对齐前填0'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: