码迷,mamicode.com
首页 > 编程语言 > 详细

python3 字符串属性总结(一)

时间:2016-03-10 23:37:23      阅读:500      评论:0      收藏:0      [点我收藏+]

标签:

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、S.capitalize() -> str   把字符串的第一个字符大写
1 >>> a=‘hello world‘
2 >>> a.capitalize()
3 ‘Hello world‘

 

2、S.center(width[, fillchar]) -> str 返回一个原字符串居中,并使用空格填充至长度width 的新字符串
1 >>> a.center(15)
2 ‘  hello world  ‘

 

3、S.count(sub[, start[, end]]) -> int 返回str 在S 里面出现的次数,
1 >>> a=‘hello world‘
2 >>> a.count(‘l‘,3)
3 2
4 >>> a.count(‘l‘,3,8)
5 1

 

4、S.casefold() 将字符串所有字符改为小写(涉及到其他语言)
     S.lower()将字符串所有字符改为小写
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

1 >>> a
2 ‘HELLO WORLD‘
3 >>> a.endswith(‘D‘)
4 True
5 >>> a.endswith(‘D‘,5)
6 True
7 >>> a.endswith(‘H‘,0,1)
8 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为可选参数,决定范围

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

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

  

 

 

 

 

 

 

 

python3 字符串属性总结(一)

标签:

原文地址:http://www.cnblogs.com/hb91/p/5263646.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!