str内置函数梳理(二)

函数名:isprintable
S.isprintable() -> bool
#如果字符串中的所有字符都是可打印的,或者字符串是空的,则isprintable()方法将返回True。如果不是,则返回False。
#在屏幕上占用打印空间的字符被称为可打印字符。 例如:
字母和符号,数字,标点,空白
>>> ''.isprintable() 
True
>>> '\n'.isprintable() 
False
>>> 's'.isprintable() 
True
>>> '\x1b'.isprintable() 
False

Return True if all characters in S are considered

printable in repr() or S is empty, False otherwise.


函数名:isspace
S.isspace() -> bool

#如果字符串中只有空格字符,则isspace()方法返回True。如果不是,则返回False。

>>> " ".isspace() 
True
>>> "aa ".isspace() 
False
Return True if all characters in S are whitespace

and there is at least one character in S, False otherwise.

函数名:istitle
S.istitle() -> bool
#如果字符串是一个标题字符串,则istitle()返回True。如果不是,则返回False。

#每个单词首字母大写(Python Is )

>>> 'I Love Python.'.istitle()
True
>>> 'I Love python.'.istitle()
False
Return True if S is a titlecased string and there is at least one
character in S, i.e. upper- and titlecase characters may only
follow uncased characters and lowercase characters only cased ones.

Return False otherwise.

函数名:isupper
S.isupper() -> bool

#字符串isupper()方法返回字符串中的所有字符是否大写。

>>> 'I Love python.'.isupper()
False
>>> 'I LOVE PYTHON.'.isupper()
True

函数名:join
S.join(iterable) -> str

#join()方法提供了一种灵活的方式来连接字符串。 它将迭代的每个元素(如列表,字符串和元组)连接到字符串并返回连接的字符串。

>>> s='Python', 'Java', 'Ruby'
>>> '->'.join(s)
'Python->Java->Ruby'
--------------------
>>> a=['a','b','c']
>>> ''.join(a)
'abc'
Return a string which is the concatenation of the strings in the

iterable.  The separator between elements is S.

函数名:ljust
S.ljust(width[, fillchar]) -> str

#返回一个给定最小宽度的左对齐字符串。类似centen(),rjust()

>>> a='abc'
>>> a.ljust(8,'@')
'abc@@@@@'
>>> 
Return S left-justified in a Unicode string of length width. Padding is

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

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

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

>>> a="ABc"
>>> a.lower()
'abc'

Return a copy of the string S converted to lowercase.


函数名:lstrip
S.lstrip([chars]) -> str

#返回一个字符串的副本,该字符串的主要字符被删除(基于字符串参数传递)。


>>> a="  abc"
>>> a.lstrip()
'abc'
>>> a="http:\\www.baidu.com"
>>> a.lstrip('http:\\')
'www.baidu.com'
Return a copy of the string S with leading whitespace removed.

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



函数名:maketrans
string.maketrans(x[, y[, z]])
Return a translation table usable for str.translate().
x - If only one argument is supplied, it must be a dictionary.
The dictionary should contain 1-to-1 mapping from a single character string to its translation OR a unicode number (97 for 'a') to its translation.
y - If two arguments are passed, it must be two strings with equal length.
Each character in the first string is a replacement to its corresponding index in the second string.
z - If three arguments are passed, each character in the third argument is mapped to None.
#maketrans()方法是一种静态方法,它创建一个字符到其翻译/替换的一对一映射。
#它为每个字符的翻译创建一个Unicode表示。

#将两字符串(一个字典),形成一一对应映射关系,返回一个字典

>>> dict = {"a": "123", "b": "456", "c": "789"}
>>> s='abc'
>>> s.maketrans(dict)
{97: '123', 98: '456', 99: '789'}
>>> s='abc'
>>> b='123'
>>> str.maketrans(s,b)
{97: 49, 98: 50, 99: 51}
>>> s=['1','2','3']
>>> a=['b','c','d']
>>> str.maketrans(s,a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: maketrans() argument 2 must be str, not list

If there are two arguments, they must be strings of equal length, and
in the resulting dictionary, each character in x will be mapped to the
character at the same position in y. If there is a third argument, it

must be a string, whose characters will be mapped to None in the result.


函数名:partition
S.partition(sep) -> (head, sep, tail)

#在参数字符串的第一次出现时分割字符串,并返回包含前分隔符、参数字符串和分离后部分的元组。如果没有找到返回本身,两个空str

>>> string = "Python is fun"
>>> string.partition('is')
('Python ', 'is', ' fun')
>>> string.partition('a')
('Python is fun', '', '')
>>> 
Search for the separator sep in S, and return the part before it,
the separator itself, and the part after it.  If the separator is not
found, return S and two empty strings.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值