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、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

  

时间: 2024-07-29 00:27:28

python3 字符串属性总结(一)的相关文章

python3 字符串属性(四)

1. S.partition(sep) -> (head, sep, tail) Search for the separator sep in S, and return the part before it, the separator itself, and the part after it. If the separator is not found, return S and two empty strings. 分割作用,参数为分割字符,分为三部分,(参数前,参数,参数后);如果参

python3 字符串属性(三)

maketrans 和 translate的用法(配合使用) 下面是python的英文用法解释 maketrans(x, y=None, z=None, /) Return a translation table usable for str.translate(). If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters to Unicode

Python3 字符串

字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 = 'Hello World!' var2 = "Runoob" Python 访问字符串中的值 Python 不支持单字符类型,单字符也在Python也是作为一个字符串使用. Python 访问子字符串,可以使用方括号来截取字符串,如下实例: 实例(Python 3.0+) #!/usr/bin/python3 var1 = '

字符串属性使用strong的原因

*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } a { color: #4183C4; } a.absent { color: #cc0000; } a.anchor { display: block; padding-left: 30px; margin-left: -30px; cursor: pointer; position: absolute

mysql 连接命令 表管理 ,克隆表,临时表,字符串属性,设定语句间的分隔符

连接和断开连接mysql -h host -u user -p (即,连接的主机.用户名和使用的密码).断开输入QUIT (或\q)随时退出: 表管理克隆表注意:create table ... like 语句不克隆表的外键定义,不克隆原表可能使用的data directory 和index directory. 和auto_increment --创建一张和现有的某张表结构一致的表.create table new_table like original_table --把某张的数据插入到克隆

JavaScript 字符串属性和方法

字符串属性: constructor : 返回创建字符串属性的函数; length : 返回字符串的长度; prototype : 允许您向对象添加属性和方法; 字符串属性: charAt() : 返回指定索引位置的字符; charCodeAt() : 返回指定索引位置字符的 Unicode 值; concat() : 连接两个或多个字符串,返回连接后的字符串; fromCharCode() : 将字符转换为 Unicode 值; indexOf() : 返回字符串中检索指定字符第一次出现的位置

python3字符串操作

python3字符串操作 1 x = 'abc' 2 y = 'defgh' 3 4 print(x + y) #x+y 5 print(x * 3) #x*n 6 print(x[2]) #x[i] 7 print(y[0:-1]) #str[i:j] 8 #求长度 >>> len(x) 11 #将其他类型转换为字符串 >>> str(123) '123' #将数字转为对应的utf-8字符 >>> chr(97) 'a' #将字符转为对应的数字 &g

python008 Python3 字符串

var1 = 'Hello World!' var2 = "QQ603374730" Python 访问字符串中的值Python 不支持单字符类型,单字符也在Python也是作为一个字符串使用.Python 访问子字符串,可以使用方括号来截取字符串,如下实例:实例(Python 3.0+) #!/usr/bin/python3 var1 = 'Hello World!' var2 = "QQ603374730" print ("var1[0]: "

[No000078]Python3 字符串操作

#!/usr/bin/env python3 # -*- coding: utf-8 -*- '''Python 字符串操作 string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等 @author: HK ''' if __name__ == '__main__': s = ' s dfg hjk,大 家好,.:?-_+0 ' #去两边空格及指定符号 print(s.strip())#两边都替换 # Return a copy of the string S with l