python3 字符串属性(一)
python3 字符串属性
1 >>> a='hello world' 2 >>> dir(a) 3 ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__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']
1 >>> a='hello world' 2 >>> a.capitalize() 3 'Hello world'
1 >>> a.center(15) 2 ' hello world '
1 >>> a='hello world' 2 >>> a.count('l',3) 3 2 4 >>> a.count('l',3,8) 5 1
1 >>> a='HELLO WORLD' 2 >>> a.casefold() 3 'hello world' 4 >>> a 5 'HELLO WORLD' 6 >>> a.lower() 7 'hello world' 8 >>> a 9 'HELLO WORLD'
5、字符串编解码
{
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode
作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编(encode)
成另一种编码。但是,Python 2.x的默认编码格式是ASCII,就是说,在没有指定Python源码编码
格式的情况下,源码中的所有字符都会被默认为ASCII码。也因为这个根本原因,在Python 2.x中
经常会遇到UnicodeDecodeError或者UnicodeEncodeError的异常。
原则
decode early, unicode everywhere, encode late,即:在输入或者声明字符串的时候,
尽早地使用decode方法将字符串转化成unicode编码格式;然后在程序内使用字符串的时候统一使用
unicode格式进行处理,比如字符串拼接、字符串替换、获取字符串的长度等操作;最后,在输出字符
串的时候(控制台/网页/文件),通过encode方法将字符串转化为你所想要的编码格式,比如utf-8等。
https://segmentfault.com/a/1190000002966978
}
S.encode(encoding='utf-8', errors='strict') -> bytes 以encoding指定的编码格式对字符串进行编码,输出的字节不是字符串,类型不同,属性不同。
6. S.endswith(suffix[, start[, end]]) -> bool
Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try.
S.startswith(prefix[, start[, end]]) -> bool
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.
1 >>> a='hello world!' 2 >>> a.endswith('!') 3 True 4 >>> a.endswith('o',0,5) 5 True 6 >>> a.startswith('h') 7 True 8 >>> a.startswith('w',6) 9 True 10 >>> a.startswith('hello') 11 True
7、 S.expandtabs(tabsize=8) -> str 把字符串的tab字符(\t)转化为空格,如不指定tabsize,默认为8个空格
1 >>> a='hello world' 2 >>> a.expandtabs() 3 'hello world' 4 >>> a='\t hello world \t' 5 >>> a.expandtabs() 6 ' hello world ' 7 >>> a.expandtabs(tabsize=2) 8 ' hello world '
8、S.find(sub[, start[, end]]) -> int 检测sub是否在字符串中,如果在则返回index,否则返回-1,start,end为可选参数,决定范围。(返回左侧第一个)
S.rfind(sub[, start[, end]]) -> int (返回右侧第一个)
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.
1 >>> a='hello world' 2 >>> a.find('h') 3 0 4 >>> a.find('h',1,3) 5 -1 6 >>> a.find('o',1,4) 7 -1 8 >>> a.find('o',1,5) 9 4
9、
S.index(sub[, start[, end]]) -> int 没有找到返回ValuueError错误 (返回左侧第一个)
Like S.find() but raise ValueError when the substring is not found. 没有找到返回 -1
S.rindex(sub[, start[, end]]) -> int (返回右侧第一个)
Like S.rfind() but raise ValueError when the substring is not found.
1 >>> a 2 'hello world' 3 >>> a.index('ll') 4 2 5 >>> a.index('lll') 6 Traceback (most recent call last): 7 File "<stdin>", line 1, in <module> 8 ValueError: substring not found
10、S.isalnum() -> bool Return True if all characters in S are alphanumeric
都是字母和数字字符返回True,否则返回False
>>> a.isalnum() False >>> a='123#$":,./' >>> a.isalnum() False >>> a='123' >>> a.isalnum() True >>> a='123a' >>> a.isalnum() True >>> a='123a(' >>> a.isalnum() False
11、S.isalpha() -> bool Return True if all characters in S are alphabetic
判断字符串是否为字母
>>> a='123' >>> b='123a' >>> c='abc' >>> a.isalpha() False >>> b.isalpha() False >>> c.isalpha() True