str内置函数梳理(三)

函数名:replace
S.replace(old, new[, count]) -> str
#replace()方法最多可使用3个参数:
您想要替换的旧的子字符串。
新的子字符串,它将替换旧的子字符串。

count(可选)——您希望用新的子字符串替换旧的子字符串的次数。

>>> song = 'Let it be, let it be, let it be, let it be'
>>> song.replace('e','@',3)
'L@t it b@, l@t it be, let it be, let it be'

Return a copy of S with all occurrences of substring.old replaced by new.  If the optional argument count is

given, only the first count occurrences are replaced.

函数名:rfind
S.rfind(sub[, start[, end]]) -> int
#从做开始查找,失败返回-1  参考find
Return the highest index in S where substring sub is found,
such that sub is contained within S[start:end].  Optional
arguments start and end are interpreted as in slice notation.

Return -1 on failure.

函数名:rindex
S.rindex(sub[, start[, end]]) -> int
#从左开始查找,失败报错ValueErro,参考index
Return the highest index in S where substring sub is found,
such that sub is contained within S[start:end].  Optional
arguments start and end are interpreted as in slice notation.

Raises ValueError when the substring is not found.

函数名:rjust
S.rjust(width[, fillchar]) -> str
#右侧填充,参考center(),ljust()
Return S right-justified in a string of length width. Padding is

done using the specified fill character (default is a space).

函数名:rpartition
S.rpartition(sep) -> (head, sep, tail)
#参考partition()
Search for the separator sep in S, starting at the end of S, and return
the part before it, the separator itself, and the part after it.  If the

separator is not found, return two empty strings and S.



函数名:rsplit
S.rsplit(sep=None, maxsplit=-1) -> list of strings
#strip()函数
Return a list of the words in S, using sep as the
delimiter string, starting at the end of the string and
working to the front.  If maxsplit is given, at most maxsplit
splits are done. If sep is not specified, any whitespace string

is a separator.

函数名:rstrip
S.rstrip([chars]) -> str
#strip()函数
Return a copy of the string S with trailing whitespace removed.

If chars is given and not None, remove characters in chars instead.



函数名:split
S.split(sep=None, maxsplit=-1) -> list of strings
#在指定的分隔符上分解一个字符串,并返回一个字符串列表。字符串转列表
#split()方法最多使用两个参数:
分隔符(可选)-是分隔符。字符串在指定的分隔符处分开。
如果分隔符没有指定,则任何空格(空格、换行等)字符串都是分隔符。
maxsplit(可选)——maxsplit定义了最大分割数。

maxsplit的默认值为-1,也就是说,对于分割的数量没有限制。

>>> text= 'Love thy neighbor'
>>> text.split()
['Love', 'thy', 'neighbor']
Return a list of the words in S, using sep as the
delimiter string.  If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any

whitespace string is a separator and empty strings areremoved from the result.

函数名:splitlines
S.splitlines([keepends]) -> list of strings

#参考split(),一每一行划分

>>> text= 'Love thy neighbor\nPython'
>>> text.splitlines()
['Love thy neighbor', 'Python']
Return a list of the lines in S, breaking at line boundaries.
Line breaks are not included in the resulting list unless keepends

is given and true.

函数名:startswith
S.startswith(prefix[, start[, end]]) -> bool
#判断以开头。返回bool.类似endswith()
Return True if S starts with the specified prefix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.

prefix can also be a tuple of strings to try.

函数名:strip
S.strip([chars]) -> str
#chars(可选)-指定要删除的字符集的字符串。

#如果没有提供chars参数,则从字符串中删除所有引导和尾随的空白。

>>> string = ' xoxo love xoxo   '
>>> string.strip()
'xoxo love xoxo'
Return a copy of the string S with leading and trailing
whitespace removed.

If chars is given and not None, remove characters in chars instead.

函数名:swapcase
S.swapcase() -> str
#大写转小写,小写转大写

>>> "THIS SHOULD ALL BE LOWERCASE.".swapcase()
'this should all be lowercase.'
>>> "THIS SHOULD All BE LOWERCASE.".swapcase()
'this should aLL be lowercase
Return a copy of S with uppercase characters converted to lowercase

and vice versa.

函数名:title
S.title() -> str

#字符串转化成标题格式,即每个单词首字母大写。

>>> 'this should all be lowercase.'.title()
'This Should All Be Lowercase.'
Return a titlecased version of S, i.e. words start with title case

characters, all remaining cased characters have lower case.

函数名:translate
S.translate(table) -> str
#字符串translate()方法返回一个字符串,其中每个字符都映射到转换表中的相应字符。

# first string

firstString = "abc"
secondString = "ghi"
thirdString = "ab"
string = "abcdef"
print("Original string:", string)
translation = string.maketrans(firstString, secondString, thirdString)
#输出
Original string: abcdef
Translated string: idef
# translate string
print("Translated string:", string.translate(translation))
# translation table - a dictionary
translation = {97: None, 98: None, 99: 105}
string = "abcdef"
print("Original string:", string)
#输出
Original string: abcdef
Translated string: idef
函数名:upper
S.upper() -> str

#将字符串中的所有小写字符转换为大写字符并返回它。

>>> 'this should all be lowercase.'.upper()
'THIS SHOULD ALL BE LOWERCASE.'

Return a copy of S converted to uppercase.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值