设为首页 加入收藏

TOP

python 字符串操作二 内建函数(一)
2017-12-23 06:07:14 】 浏览:350
Tags:python 字符串 操作 函数

一、查看字符串的内建函数

>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__',
'__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format',
'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace',
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition',
'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
In [1]: a = '123'

In [2]: a.
a.capitalize    a.endswith      a.index         a.isidentifier  a.istitle       a.lstrip        a.rindex        a.split         a.title
a.casefold      a.expandtabs    a.isalnum       a.islower       a.isupper       a.maketrans     a.rjust         a.splitlines    a.translate
a.center        a.find          a.isalpha       a.isnumeric     a.join          a.partition     a.rpartition    a.startswith    a.upper
a.count         a.format        a.isdecimal     a.isprintable   a.ljust         a.replace       a.rsplit        a.strip         a.zfill
a.encode        a.format_map    a.isdigit       a.isspace       a.lower         a.rfind         a.rstrip        a.swapcase 

二、常用的字符串内建函数

1、capitalize,字符串的第一个字符大写

>>> a = 'today is a good day.'
>>> a.capitalize()
'Today is a good day.'

2、 casefold,将所有字符小写,Unicode所有字符均适用

>>> b
'TODAY IS A GOOD DAY.'
>>> b.casefold()
'today is a good day.'

3、lower,将所有字符小写,只适用ASCii

>>> b
'TODAY IS A GOOD DAY.'
>>> b.lower()
'today is a good day.'

4、upper,将所有字符大写

>>> a
'today is a good day.'
>>> a.upper()
'TODAY IS A GOOD DAY.'

5、center,返回一个原字符串居中,并使用空格填充至长度 width 的新字符串,语法:str.center(width[, fillchar])

>>> a
'today is a good day.'
>>> a.center(40)
'          today is a good day.          '

6、count,用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置,语法:str.count(sub, start= 0,end=len(string))

>>> a
'today is a good day.'
>>> a.count('a')
3
>>> a.count('a', 5, -2)
2

7、encode,以 encoding 指定的编码格式编码字符串。errors参数可以指定不同的错误处理方案,语法:str.encode(encoding='UTF-8',errors='strict')

errors -- 设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通过 codecs.register_error() 注册的任何值。

>>> c = '你好'
>>> c.encode(encoding='utf-8')
b'\xe4\xbd\xa0\xe5\xa5\xbd'

8、decode,以 encoding 指定的编码格式解码字符串。默认编码为字符串编码,语法:str.decode(encoding='UTF-8',errors='strict')

>>> d
b'\xe4\xbd\xa0\xe5\xa5\xbd'
>>> d.decode(encoding='utf-8')
'你好'

9、startwith,检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查,语法:str.startswith(str, beg=0,end=len(string))

>>> a
'today is a good day.'
>>> a.startswith('today')
True
>>> a.startswith('day')
False
>>> a.startswith('day', 5)
False
>>> a.startswith('today', 5)
False

10、endwith,判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回Fal

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Matplotlib库的使用 下一篇python 字符串函数功能快查

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目