python中的isprintable函数_函数解析——python学习第三次总结

.str字符串类型(二)

1.  isalpha  判断目标字符串中是否只含字母

表达式  str.isalpha()  ==>  bool

示例:

1 a = 'alex'

2 v =a.isalpha()3 print(v)

# 输出

# True

源码:

1 def isalpha(self, *args, **kwargs): #real signature unknown

2 """

3 Return True if the string is an alphabetic string, False otherwise.4

5 A string is alphabetic if all characters in the string are alphabetic and there6 is at least one character in the string.7 """

源码

2.  isdecimal

isdigit  都是用来判断目标字符串是否是数字,上面这个是在十进制范围内,下面这个不光能判断纯文本,还能判断复杂符号。

表达式  str.isdecimal()  ==>  bool

str.isdigit()  ==>  bool

示例:

1 a = '②'

2 b =a.isdecimal()3 c =a.isdigit()4 print(b,c)

# 输出

# false true

源码:

1 def isdecimal(self, *args, **kwargs): #real signature unknown

2 """

3 Return True if the string is a decimal string, False otherwise.4

5 A string is a decimal string if all characters in the string are decimal and6 there is at least one character in the string.7 """

isdecimal

1 def isdigit(self, *args, **kwargs): #real signature unknown

2 """

3 Return True if the string is a digit string, False otherwise.4

5 A string is a digit string if all characters in the string are digits and there6 is at least one character in the string.7 """

isdigit

3.  isidentifier  判断目标字符串是否只含数字、字母、下划线,即判断出是否是属于变量名称。

表达式  str.isidentifier()  ==>  bool

示例:

1 print('def_class123'.isidentifier())

# 输出

# true

源码

1 def isidentifier(self, *args, **kwargs): #real signature unknown

2 """

3 Return True if the string is a valid Python identifier, False otherwise.4

5 Use keyword.iskeyword() to test for reserved identifiers such as "def" and6 "class".7 """

源码

4.  isspace  目标字符串中是否只含有空格

表达式  str.isspace()  ==>  bool

示例:

1 print(' '.isspace())

# 输出

# true

源码:

1 def isspace(self, *args, **kwargs): #real signature unknown

2 """

3 Return True if the string is a whitespace string, False otherwise.4

5 A string is whitespace if all characters in the string are whitespace and there6 is at least one character in the string.7 """

源码

5.  islower  判断目标字符串中的所有英文字母是否是小写

表达式  str.islower()  ==>  bool

示例:

1 print('adsf_②123贰二'.islower())

# 输出

# TRUE

源码:

1 def islower(self, *args, **kwargs): #real signature unknown

2 """

3 Return True if the string is a lowercase string, False otherwise.4

5 A string is lowercase if all cased characters in the string are lowercase and6 there is at least one cased character in the string.7 """

源码

6.  isnumeric  判断目标字符串是否只含数字(含字符数字)

表达式  str.isnumeric()  ==>  bool

示例:

1 print('②Ⅱ123贰二'.isnumeric())

# 输出

# true

源码:

1 def isnumeric(self, *args, **kwargs): #real signature unknown

2 """

3 Return True if the string is a numeric string, False otherwise.4

5 A string is numeric if all characters in the string are numeric and there is at6 least one character in the string.7 """

源码

7.  isprintable  判断字符串是否可打印(是否可以打印决定于里面是否有不可显示字符。换言之里面不可复用函数,需先输出结果)

表达式  str.isprintable()  ==>  bool

示例:

1 print('er二贰②——'.isprintable())

# 输出

# TRUE

源码:

1 def isprintable(self, *args, **kwargs): #real signature unknown

2 """

3 Return True if the string is printable, False otherwise.4

5 A string is printable if all of its characters are considered printable in6 repr() or if it is empty.7 """

源码

8.  istitle  目标字符串中是否是标题(即满足每个单词首字母大写)

表达式  str.istitle()  ==>  bool

示例:

1 print('Return True if the string is a valid Python identifier'.istitle())

# 输出

# false

源码:

1 def istitle(self, *args, **kwargs): #real signature unknown

2 """

3 Return True if the string is a title-cased string, False otherwise.4

5 In a title-cased string, upper- and title-case characters may only6 follow uncased characters and lowercase characters only cased ones.7 """

源码

9.  title  将目标字符串转换为标题格式(即转换为每个单词首字母大写)

表达式:  str.title()  ==>  str

示例:

1 print('Return True if the string is a valid Python identifier'.title())

# 输出

# Return True If The String Is A Valid Python Identifier

源码:

1 def title(self, *args, **kwargs): #real signature unknown

2 """

3 Return a version of the string where each word is titlecased.4

5 More specifically, words start with uppercased characters and all remaining6 cased characters have lower case.7 """

源码

10.  join  将设置字符插入目标字符串中每个字符中间

表达式  str.join(iterable)  ==>  str

示例:

1 print('-'.join('你是风儿我是沙'))

# 输出

# 你-是-风-儿-我-是-沙

源码:

1 def join(self, ab=None, pq=None, rs=None): #real signature unknown; restored from __doc__

2 """

3 Concatenate any number of strings.4

5 The string whose method is called is inserted in between each given string.6 The result is returned as a new string.7

8 Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'9 """

源码

11.  ljust  原有字符串在左方,右方添加指定字符

表达式  str.ljust(width,fillchar)  ==>  str

width  总格式化宽度

fillchar  插入字符

示例:

1 print('alex'.ljust(20,'$'))

# 输出

# alex$$$$$$$$$$$$$$$$

源码:

1 def ljust(self, *args, **kwargs): #real signature unknown

2 """

3 Return a left-justified string of length width.4

5 Padding is done using the specified fill character (default is a space).6 """

源码

12.  rjust  原有字符串在右方,左方添加指定字符

表达式  str.rjust(width,fillchar)  ==>  str

width  总格式化宽度

fillchar  插入字符

示例:

1 print('alex'.rjust(20,'$'))

# 输出

# $$$$$$$$$$$$$$$$alex

源码:

1 def rjust(self, *args, **kwargs): #real signature unknown

2 """

3 Return a right-justified string of length width.4

5 Padding is done using the specified fill character (default is a space).6 """

源码

13.  zfill  原有字符串在左方,右方添加指定字符

表达式  str.zfill(width)  ==>  str

width  输出字符串所占用总宽度

示例:

1 print('alex'.zfill(20))

# 输出

# 0000000000000000alex

源码:

1 def zfill(self, *args, **kwargs): #real signature unknown

2 """

3 Pad a numeric string with zeros on the left, to fill a field of the given width.4

5 The string is never truncated.6 """

源码

14.  lower  将目标字符串中所有字母转换为小写

表达式  str.lower()  ==>  str

示例:

1 print('alex二②贰'.lower())

# 输出

# alex二贰②

源码:

1 def lower(self, *args, **kwargs): #real signature unknown

2 """Return a copy of the string converted to lowercase."""

源码

15.  upper  将目标字符串中所有字母转换为大写

表达式  str.upper()  ==>  str

示例:

1 print('Alex二贰②'.upper())

# 输出

# ALEX二贰②

源码:

1 def upper(self, *args, **kwargs): #real signature unknown

2 """Return a copy of the string converted to uppercase."""

源码

16.  isupper  判断目标字符串中所有字母是否为大写

表达式  str.isupper()  ==>  str

示例:

1 print('ALEX二贰②'.isupper())

# 输出

# TRUE

源码:

1 def isupper(self, *args, **kwargs): #real signature unknown

2 """

3 Return True if the string is an uppercase string, False otherwise.4

5 A string is uppercase if all cased characters in the string are uppercase and6 there is at least one cased character in the string.7 """

源码

17.  strip  左右同时开始向中间祛设置字符串

表达式  str.strip(chars)  ==>  str

str  需祛除的字符串

chars  源字符串

示例:

1 print('alexalexalexalex'.strip('x''a'))

# 输出

# lexalexalexale

源码:

1 def strip(self, *args, **kwargs): #real signature unknown

2 """

3 Return a copy of the string with leading and trailing whitespace remove.4

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

源码

18.  lstrip  从左开始向右祛设置字符串

表达式  str.lstrip(chars)  ==>  str

str  需祛除的字符串

chars  源字符串

示例:

1 print('alexalexalexalex'.lstrip('x''a'))

# 输出

# lexalexalexalex

源码:

1 def lstrip(self, *args, **kwargs): #real signature unknown

2 """

3 Return a copy of the string with leading whitespace removed.4

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

源码

19.  rstrip  从右开始向左祛设置字符串

表达式  str.rstrip(chars)  ==>  str

str  需祛除的字符串

chars  源字符串

示例:

1 print('alexalexalexalex'.rstrip('x''a'))

# 输出

# alexalexalexale

源码:

1 def rstrip(self, *args, **kwargs): #real signature unknown

2 """

3 Return a copy of the string with trailing whitespace removed.4

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

源码

20.  maketrans  创建一个翻译关系

表达式  str.maketrans(from,to)  ==>  str

from  源字符串中需替换字符

to  需替换为的字符

示例:(一般和translate复用)

源码:

1 def maketrans(self, *args, **kwargs): #real signature unknown

2 """

3 Return a translation table usable for str.translate().4

5 If there is only one argument, it must be a dictionary mapping Unicode6 ordinals (integers) or characters to Unicode ordinals, strings or None.7 Character keys will be then converted to ordinals.8 If there are two arguments, they must be strings of equal length, and9 in the resulting dictionary, each character in x will be mapped to the10 character at the same position in y. If there is a third argument, it11 must be a string, whose characters will be mapped to None in the result.12 """

源码

21.  translate  按照对应关系翻译,并可过滤设定字符

表达式  str.translate(table)  ==>  str

table  对应关系,一般由maketrans创建

示例:

1 a = 'asdfghjkl②'

2 b = str.maketrans('aldf②','12349')3 c =a.translate(b)4 print(c)

# 输出

# 1s34ghjk29

源码:

1 def translate(self, *args, **kwargs): #real signature unknown

2 """

3 Replace each character in the string using the given translation table.4

5 table6 Translation table, which must be a mapping of Unicode ordinals to7 Unicode ordinals, strings, or None.8

9 The table must implement lookup/indexing via __getitem__, for instance a10 dictionary or list. If this operation raises LookupError, the character is11 left untouched. Characters mapped to None are deleted.12 """

源码

22.  partition  按既定字符分割一次,从左开始

表达式  str.partition(sep)  ==>  tuple

sep  用以分割的字符

示例:

1 print('asdfghfjkl;'.partition('f'))

# 输出

# ('asd', 'f', 'ghfjkl;')

源码:

1 def partition(self, *args, **kwargs): #real signature unknown

2 """

3 Partition the string into three parts using the given separator.4

5 This will search for the separator in the string. If the separator is found,6 returns a 3-tuple containing the part before the separator, the separator7 itself, and the part after it.8

9 If the separator is not found, returns a 3-tuple containing the original string10 and two empty strings.11 """

源码

23.  rpartition  按既定字符分割一次,从右开始

表达式  str.partition(sep)  ==>  tuple

sep  用以分割的字符

示例:

1 print('asdfghfjkl;'.rpartition('f'))

# 输出

# ('asdfgh', 'f', 'jkl;')

源码:

1 def rpartition(self, *args, **kwargs): #real signature unknown

2 """

3 Partition the string into three parts using the given separator.4

5 This will search for the separator in the string, starting at the end. If6 the separator is found, returns a 3-tuple containing the part before the7 separator, the separator itself, and the part after it.8

9 If the separator is not found, returns a 3-tuple containing two empty strings10 and the original string.11 """

源码

24.  split  按既定字符将目标字符串内全部对应分割,默认从左开始,可指定分割次数(分割后对应分割符不会返回)

表达式  str.split(sep,maxsplit)  ==>  list

sep  指定的分隔符

maxsplit  最大分割次数

示例:

1 print('asdfasdfadsfdsfdsfadfasdfasdfasd'.split('d',5))

# 输出

# ['as', 'fas', 'fa', 'sf', 'sf', 'sfadfasdfasdfasd']

源码:

1 def split(self, *args, **kwargs): #real signature unknown

2 """

3 Return a list of the words in the string, using sep as the delimiter string.4

5 sep6 The delimiter according which to split the string.7 None (the default value) means split according to any whitespace,8 and discard empty strings from the result.9 maxsplit10 Maximum number of splits to do.11 -1 (the default value) means no limit.12 """

源码

25.  rspit  按既定字符将目标字符串内全部对应分割,从右开始,可指定分割次数(分割后对应分割符不会返回)

表达式  str.split(sep,maxsplit)  ==>  list

sep  指定的分隔符

maxsplit  最大分割次数

示例:

1 print('asdfasdfadsfdsfdsfadfasdfasdfasd'.rsplit('d',5))

# 输出

# ['asdfasdfadsfdsf', 'sfa', 'fas', 'fas', 'fas', '']

源码:

1 def rsplit(self, *args, **kwargs): #real signature unknown

2 """

3 Return a list of the words in the string, using sep as the delimiter string.4

5 sep6 The delimiter according which to split the string.7 None (the default value) means split according to any whitespace,8 and discard empty strings from the result.9 maxsplit10 Maximum number of splits to do.11 -1 (the default value) means no limit.12

13 Splits are done starting at the end of the string and working to the front.14 """

源码

26.  splitlines  根据“\n”分隔符分割目标字符串

表达式  str.splitlines(bool)  ==>  list

bool  选填布尔值确认是否保留换行符,默认为Flase,首字母一定要大写

示例:

1 print('asd\nasdfads\ndsfdsf\nfasdfa\nfasd'.splitlines(True))

# 输出

# ['asd\n', 'asdfads\n', 'dsfdsf\n', 'fasdfa\n', 'fasd']

源码:

1 def splitlines(self, *args, **kwargs): #real signature unknown

2 """

3 Return a list of the lines in the string, breaking at line boundaries.4

5 Line breaks are not included in the resulting list unless keepends is given and6 true.7 """

源码

27.  swapcase  转换目标字符串中字母的大小写

表达式  str.swapcase()  ==>  str

示例:

1 print('aLEx②贰'.swapcase())

# 输出

# AleX②贰

源码:

1 def swapcase(self, *args, **kwargs): #real signature unknown

2 """Convert uppercase characters to lowercase and lowercase characters to uppercase."""

源码

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值