python之字符串类详解

更新时间:2023-07-26 20:28:04 阅读: 评论:0

python之字符串类详解
在python中,加了引号的字符被认为是字符串。
1、capitalize()
把字符串的第⼀个字符⼤写
>>> s = "hello"
>>> s.capitalize()
'Hello'
2、cafold()
转换字符串中所有⼤写字符为⼩写,两者的区别是:lower() ⽅法只对ASCII编码,也就是‘A-Z’有效,对于其他语⾔(⾮汉语或英⽂)中把⼤写转换为⼩写的情况只能⽤ cafold() ⽅法。
>>> s = "HELLO"
>>> s.cafold()司马迁的故事
'hello'
3、center(width,fillchar='')
center() 返回⼀个原字符串居中,并使⽤空格填充⾄长度 width 的新字符串。默认填充字符为空格。下⾯以填充"*"举例:
>>> s = "hello"
>>> s.center(10,"*")
'**hello***
4、count(str, beg=0, end=len(string))
返回 str 在 string ⾥⾯出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数
>>> s="hello"
>>> s.count("l")
2
5、encode(lf, /, encoding='utf-8', errors='strict')
回报英文
以 encoding 指定的编码格式解码 string,如果出错默认报⼀个 ValueError 的异常,除⾮ errors 指定的是 'ignore' 或者'replace'
>>> s = "你好"
>>> s.encode()
b'\xe4\xbd\xa0\xe5\xa5\xbd'
>>> s = "hello"
>>> s.encode()
b'hello'
>>>
6、dswith(obj, beg=0, end=len(string))
检查字符串是否以 obj 结束,如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束,如果是,返回 True,否则返回 Fal.
如下例判断字符串“hello”是否以字符“o”结束
>>> s="hello"
>>> s.endswith("o")
True
>>> s.endswith("a")
Fal
7、pandtabs(tabsize=8)
把字符串 string 中的 tab 符号转为空格,tab 符号默认的空格数是 8。
>>> s=" this is a tap"
>>> s.expandtabs()
' this is a tap'
8、str.find(str, beg=0, end=len(string))
检测 str 是否包含在 string 中,如果 beg 和 end 指定范围,则检查是否包含在指定范围内,如果是返回开始的索引值,否则返回-1
>>> s.find("a")
-1
>>> s.find("h")
9、str.format(*args, **kwargs)
格式化字符串
{}个数等于参数
>>> "The sum of 1 + 2 is {0}".format(1+2)
'The sum of 1 + 2 is 3'
>>> print("my name is {},i am {} years old".format("kevin",10))
my name is kevin,i am 10 years old
{}个数⼤于参数
>>> print("my name is {},i am {} years old".format("kevin",10,"hello"))
my name is kevin,i am 10 years old
{}个数⼩于参数
>>> print("my name is {},i am {} years old".format("kevin"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
10、str.format_map(mapping)
类似 str.format(*args, **kwargs) ,不同的是 mapping 是⼀个字典对象。
>>> People = {"name": "john", "age": 33}
>>> print("My name is {name},iam {age} old".format_map(People))
My name is john,iam 33 old
11、str.index(str, beg=0, end=len(string))
跟find()⽅法⼀样,只不过如果str不在 string中会报⼀个异常.
>>> s="hello"
>>> s.index("2")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> s.index("e")
1
12、str.isalnum()
如果 string ⾄少有⼀个字符并且所有字符都是字母或数字则返回 True,否则返回 Fal >>> s = "hello"
>>> s.isalnum()
True
>>> s="!@#$"
>>> s.isalnum()
Fal
13、str.isalpha()
如果 string ⾄少有⼀个字符并且所有字符都是字母则返回 True,
否则返回 Fal
>>> s="123456"
>>> s.isalpha()
Fal
人体重要穴位14、str.isascii()
如果字符串为空或字符串中的所有字符都是 ASCII,则返回 True,否则返回 Fal。
>>> s="hello"
>>> s.isascii()
True
>>> s="中国"
>>> s.isascii()
Fal
15、str.isdecimal()
如果 string 只包含⼗进制数字则返回 True 否则返回 Fal.
>>> s="1234"
>>> s.isdecimal()
True
>>> s="123hello"
>>> s.isdecimal()
Fal
16、str.isdigit()
如果 string 只包含数字则返回 True 否则返回 Fal.
>>> s="12345"
>>> s.isdigit()
True
>>> s="123aed"
>>> s.isdigit()
Fal
17、str.isidentifier()
⽤于判断字符串是否是有效的 Python 标识符,可⽤来判断变量名是否合法。如果字符串是有效的 Python 标识符返回 True,否则返回Fal。
>>> s="hello"
>>> s.isidentifier()
True
>>> s="123"
>>> s.isidentifier()
Fal
18、str.islower()
如果 string 中包含⾄少⼀个区分⼤⼩写的字符,并且所有这些(区分⼤⼩写的)字符都是⼩写,则返回 True,否则返回 Fal
>>> s="hello"
>>> s.islower()
True
>>> s="Hello"
>>> s.islower()
Fal
19、str.isnumeric()
如果 string 中只包含数字字符,则返回 True,否则返回 Fal
>>> s="123456"
>>> s.isnumeric()
True
>>> s="123hello"
>>> s.isnumeric()
Fal
20、str.isprintable()
大同有什么好玩的地方如果字符串中的所有字符都可打印或字符串为空,则返回 True,否则返回 Fal。
⾮打印字符是指在 Unicode 字符数据库中定义为“其他”或“分隔符”的字符,但 ASCII 空格(0x20)除
外,它被认为是可打印的。(请注意,此上下⽂中的可打印字符是在对字符串调⽤ repr( ) 时不应转义的字符。它与写⼊的字符串的处理⽆关系统标准输出或者系统标准。)
>>> s="hello"
>>> s.isprintable()
True
>>> s="\nabc"
>>> s.isprintable()
Fal
21、str.isspace()
如果 string 中只包含空格,则返回 True,否则返回 Fal.
>>> s="hello world"
>>> s.isspace()
Fal
>>> s=" "
>>> s.isspace()
True
>>> s="  "
>>> s.isspace()房屋抵押合同范本
True
22、str.istitle()
检测字符串中所有的单词拼写⾸字母是否为⼤写,且其他字母为⼩写。
>>> s="hello world"
>>> s.istitle()
Fal
>>> s="Hello world"
>>> s.istitle()
Fal
>>> s="Hello World"
独字成语
>>> s.istitle()
True
23、str.isupper()
如果 string 中包含⾄少⼀个区分⼤⼩写的字符,并且所有这些(区分⼤⼩写的)字符都是⼤写,则返回 True,否则返回 Fal
>>> s="hello world"
>>> s.isupper()
Fal
>>> s="Hello World"
>>> s.isupper()
Fal
>>> s="HELLO"
>>> s.isupper()
True
24、str.join(iterable)
以 str作为分隔符,将 iterable中所有的元素(的字符串表⽰)合并为⼀个新的字符串
>>> s = ("hello","world")
>>> "-".join(s)
'hello-world'
25、str.ljust(width[, fillchar])
返回⼀个原字符串左对齐,并使⽤空格填充⾄长度 width 的新字符串
>>> s="hello world"
>>> s.ljust(20,"*")
'hello world*********'
26、str.lower()
转换 string 中所有⼤写字符为⼩写.
>>> s="Hello World"
>>> s.lower()
'hello world'
查字典音序是什么
27、str.lstrip([chars])
截掉 string 左边的空格
>>> s=" Hello World"
>>> s.lstrip()
'Hello World'
观音菩萨坐骑28、static str.maketrans(x[, y[, z]])
maketrans() ⽅法⽤于创建字符映射的转换表,对于接受两个参数的最简单的调⽤⽅式,第⼀个参数是字符串,表⽰需要转换的字符,第⼆个参数也是字符串表⽰转换的⽬标。
原始字符串是"hello world",创建字符映射,e⽤1代替,l⽤2代替
>>> s="hello world"
>>> s_in = "el"
>>> s_out = "12"
>>> trantab = str.maketrans(s_in,s_out)
>>> s.translate(trantab)
'h122o wor2d'
29、str.partition(p)
有点像 find()和 split()的结合体,从p出现的第⼀个位置起,把字符串 string 分成⼀个 3 元素的元组 (string_pre_str,str,string_post_str),如果 string 中不包含str则 string_pre_str == string.
>>> s="hello world"
>>> s.partition("w")
('hello ', 'w', 'orld')
30、place(str1, str2,  unt(str1))
把 string 中的 str1 替换成 str2,如果 num 指定,则替换不超过 num 次.
>>> s="hello world"
>>> s.replace("l","9")
'he99o wor9d'
>>> s.replace("l","9",2)
'he99o world'
31、str.rfind(sub[, start[, end]])
类似于 find() 函数,返回字符串最后⼀次出现的位置,如果没有匹配项则返回 -1。
>>> s="hello world"
>>> s.rfind("w")
6
>>> s.rfind("k")
-1
32、str.rindex(sub[, start[, end]])
类似于 index(),不过是从右边开始.
>>> s="hello world"
>>> s.rindex("r")
8
33、str.rjust(width[, fillchar])
返回⼀个原字符串右对齐,并使⽤空格填充⾄长度 width 的新字符串
>>> s="hello world"
>>> s.rjust(20,"*")
'*********hello world'

本文发布于:2023-07-26 20:28:04,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1118790.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:字符串   字符   返回   指定   是否   空格
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图