String built-in functions for Python 3.6

1、capitalize(self)

    Convert the first character of a string to uppercase

2、casefold(self)

    Returns the string generated by converting all uppercase characters in the string to lowercase

3、center(self, width, fillchar=None)

    Returns a string with the specified width centered, fillchar is the filled character, the default is space

4、count(self, sub, start=None, end=None)

    Returns the number of occurrences of sub in the string, if start or end is specified, returns the number of occurrences of sub within the specified range

5、encode(self, encoding='utf-8', errors='strict')

    Encode the string in the encoding format specified by encoding. If an error occurs, a ValueError exception will be reported by default, unless errors specify 'ignore' or 'replace'

6、endswith(self, suffix, start=None, end=None)

    Check whether the string ends with suffix, if start or end is specified, check whether the specified range ends with suffix, if so, return True, otherwise return False

7、expandtabs(self, tabsize=8)

    Convert the tab symbols in the string string to spaces, the default number of spaces for the tab symbol is 8

8、find(self, sub, start=None, end=None)

    Check if sub is included in the string, if the range start and end are specified, check if it is included in the specified range, if it is included, return the index value of the start, otherwise return -1

9、format(self, *args, **kwargs)

    Example: 1. Use positional parameters, pass in a list of positional parameters available *list

          1、'my name is {} ,age {}'.format('hoho',18)

            2、'my name is {1} ,age {0}'.format(10,'hoho')

            3, li = ['hoho', 18]

            'my name is {} ,age {}'.format(*li)

        2. Use keyword parameters. The value of the keyword parameter must match. You can use a dictionary as the keyword parameter to pass in the value. Add ** before the dictionary.

          1、'my name is {name},age is {age}'.format(name='hoho',age=19)

          2、hash = {'name':'hoho','age':18}

            'my name is {name},age is {age}'.format(**hash)

        3. Fill and format, precision and base, use index

10、format_map(self, mapping)

    Similar to format(self, *args, **kwargs), except that mapping is a dictionary object.

    Example: hash = {'name':'hoho','age': 18}

         'my name is {name},age is {age}'.format_map(hash)

11、index(self, sub, start=None, end=None)

    跟find()方法一样,只不过如果str不在字符串中会报一个异常.

12、isalnum(self)

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

13、isalpha(self)

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

14、isdecimal(self)

    如果字符串是否只包含十进制字符返回True,否则返回False。

15、isdigit(self)

    如果字符串只包含数字则返回 True 否则返回 False

16、isidentifier(self)

    判断字符串是否可为合法的标识符

17、islower(self)

    如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False

18、isnumeric(self)

    如果字符串中只包含数字字符,则返回 True,否则返回 False

19、isprintable(self)

    判断是否为可打印字符串,是返回 True,否则返回 False

20、isspace(self)

    判断是否为空格,是返回 True,否则返回 False

21、istitle(self)

    判断是否首字母大写,其他字母小写,是返回 True,否则返回 False

22、issupper(self)

    如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False

23、str.join(self, iterable)

    以指定字符串str作为分隔符,将 iterable中所有的元素(字符串表示)合并为一个新的字符串,iterable必须是每一个元素是字符串,否则会跑出TypeError异常

24、ljust(self, width, fillchar=None)

    返回一个原字符串左对齐,并使用 fillchar 填充至长度 width 的新字符串,fillchar 默认为空格

25、lower(self)

    转换字符串中所有大写字符为小写

26、lstrip(self, chars=None)

    截掉字符串左边的指定字符chars,默认为空格

27、maketrans(self, *args, **kwargs)

    创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标

     示例:1、a = 'dobi is a dog'

         table = str.maketrans('dobi', 'alph')

 

         a.translate(table)

 

         # 'alph hs a alg'
28、partition(self, sep)
    返回分隔符前的部分,分隔符,分隔符后的部分。如果没找到分隔符,则返回字符串本身以及两个空字符串
29、replace(self, old, new, count=None)
    替换count个old字符为新的new字符,count默认为全部
30、rfind(self, sub, start=None, end=None)
    类似于 find()函数,不过是从右边开始查找.
31、rindex(self, sub, start=None, end=None)
    类似于 index(),不过是从右边开始
32、rjust(self, width, fillchar=None)
    返回一个原字符串右对齐,并使用 fillchar 填充至长度 width 的新字符串,fillchar 默认为空格
33、rpartition(self, sep)
    类似于 partition()函数,但从右往左搜索
34、rsplit(self, sep=None, maxsplit=-1)
    类似于 split()函数, 但从右往左
35、rstrip(self, chars=None)
    截掉字符串右边的指定字符chars,默认为空格
36、split(self, sep=None, maxsplit=-1)
    分割字符串,指定sep为分隔符,maxsplit为最大分隔符。0表示不分割,1表示分割成2段
37、splitlines(self, keepends=None)
    按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符
38、startswith(self, prefix, start=None, end=None)
    检查字符串是否是以 prefix开头,是则返回 True,否则返回 False。如果start和 end 指定值,则在指定范围内检查
39、strip(self, chars=None)
    截掉字符串左边和右边的指定字符chars,默认为空格
40、swapcase(self)
    将字符串中大写转换为小写,小写转换为大写
41、title(self)
    返回首字母大写,其他所有字母小写的字符串
42、translate(self, table)
    参照maketrans()函数的示例
43、upper(self)
    转换字符串中的小写字母为大写
44、zfill(self, width)
    用0填补总长度为width的字符串的左边,如果width小于字符串的长度,则原样返回

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325215712&siteId=291194637