python 3入门之 字符串方法
2017-12-16 15:22
573 查看
1、capitalize()将字符串的第一个字符改为大写
2、casefold()将字符串所有字符改为小写
3、center(width)将字符串居中,并用空格字符串填充至width长度,空格均匀分布在两侧,当width
4、count(str[,start[,end]])返回str在字符串中出现的次数,start,end为可选参数,决定范围
5、encode(encoding=”utf-8”,errors=”strict”)以encoding指定的方式对字符串进行编码。
7、expandtabs([tabsize = 8])把字符串的tab字符(\t)转化为空格,如不指定tabsize,默认为8个空格
8、find(str[,start[,end]]) 检查str是否在字符串中,如果在则返回字符串第一次出现的index,否则返回-1,start和end为可选参数,决定范围
rfind(str[,start[,end]]) 检查str是否在字符串中,如果在则返回字符串从右开始第一次出现的index,否则返回-1,start和end为可选参数,决定范围
9、index(str[,start[,end]])类似find(),不同的事若str不在字符中,返回的不是-1,而是异常
rindex(str[,start[,end]])类似rfind(),不同的事若str不在字符中,返回的不是-1,而是异常
10、isalnum()如果字符串只包含字母和数字,并且为非空,返回True,否则False
11、isalpha()如果字符串均为字母,且非空,返回True,否则False
12、isdigit()
13、isdecimal()
14、isnumeric()
对数字的判断
15、islower()如果字符串均为小写,且非空,返回True,否则False
16、isupper()如果字符串均为大写,且非空,返回True,否则Flase
17、isdentifier()判断字符串是够包含该语言的保留字
18、isprintable()判断字符串中所有字符串是否可打印,或字符串是空,都返回True,否则False
19、isspace()判断字符串是否只包含空格、制表符和换行,且为非空。
20、istitle() 判断标题模式,以大写字母开头,后面均为小写的单词返回True,否则False
21、join()用指定字符将所有元素合并为一个新的字符串
22、lower()转化字符串中所有大写为小写
23、startwith()和endwith()如果字符串以该方法传入的字符串开始或结尾,则返回True,否则返回False
24、split()使用该方法传入的字符分隔原字符串,可指定分隔次数,返回一个字符串列表,默认为空格。
rsplit(),与split作用相同,但是从右侧开始
25、rjust()、ljust()和center()文本对齐方法
26、strip()、rstrip()和lstrip()删除空白字符。
27、partition()在指定字符串中找str,如果找到则返回该字符前面部分
rpartition()与partition()类似,但从右边开始查找
28、replace()用第二参数字符串替换第一参数字符串,如果不指定次数,则全部替换
29、splitlines(),按换行符,对字符串进行分隔,如果没有指定keepends=True,则会将其从结果中移除
30、swapcase() 大小写互相转换
31、zfill()这里Z值zero,用0将字符填充到指定长度
>>>s = "i love you" >>>s.capitalize "I love you"
2、casefold()将字符串所有字符改为小写
>>>(s.capitalize).casefold() "i love you"
3、center(width)将字符串居中,并用空格字符串填充至width长度,空格均匀分布在两侧,当width
>>>s.center(30) ' i love you '
4、count(str[,start[,end]])返回str在字符串中出现的次数,start,end为可选参数,决定范围
>>>s.count("o") 2 >>>s.count("o",5) 1 >>>s.count("o",7,len(s)) 1
5、encode(encoding=”utf-8”,errors=”strict”)以encoding指定的方式对字符串进行编码。
待完善
7、expandtabs([tabsize = 8])把字符串的tab字符(\t)转化为空格,如不指定tabsize,默认为8个空格
>>> s = "\t i love you \t" >>> s.expandtabs() ' i love you ' >>> s.expandtabs(tabsize = 4) ' i love you '
8、find(str[,start[,end]]) 检查str是否在字符串中,如果在则返回字符串第一次出现的index,否则返回-1,start和end为可选参数,决定范围
rfind(str[,start[,end]]) 检查str是否在字符串中,如果在则返回字符串从右开始第一次出现的index,否则返回-1,start和end为可选参数,决定范围
>>> s = "i love you" >>> s.find("o") 3 >>> s.find("o",3) 3 >>> s.find("o",4,13) 8 >>> s.find("a") -1 >>> "this is an apple".rfind("a") 11 >>> "this is an apple".rfind("a",12) -1 >>> "this is an apple".rfind("a",5,10) 8
9、index(str[,start[,end]])类似find(),不同的事若str不在字符中,返回的不是-1,而是异常
rindex(str[,start[,end]])类似rfind(),不同的事若str不在字符中,返回的不是-1,而是异常
>>> s.index("o") 3 >>> s.index("o",3) 3 >>> s.index("o",4,13) 8 >>> s.index("a") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found >>> "this is an apple".rindex("a") 11 >>> "this is an apple".rindex("a",12) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found >>> "this is an apple".rindex("a",5,10) 8
10、isalnum()如果字符串只包含字母和数字,并且为非空,返回True,否则False
>>> "i love you".isalnum() False >>> "iloveyou".isalnum() True
11、isalpha()如果字符串均为字母,且非空,返回True,否则False
>>> "iloveyou".isalpha() True >>> "iloveyou1".isalpha() False >>> "i love you".isalpha() False
12、isdigit()
13、isdecimal()
14、isnumeric()
对数字的判断
>>>num = "1" #unicode >>>num.isdigit() # True >>>num.isdecimal() # True >>>num.isnumeric() # True >>>num = b"1" # byte >>>num.isdigit() # True >>>num.isdecimal() # AttributeError 'bytes' object has no attribute 'isdecimal' >>>num.isnumeric() # AttributeError 'bytes' object has no attribute 'isnumeric' >>> num = "四" >>> num.isdigit() False >>> num.isdecimal() False >>& 4000 gt; num.isnumeric() True
15、islower()如果字符串均为小写,且非空,返回True,否则False
16、isupper()如果字符串均为大写,且非空,返回True,否则Flase
>>> "i love you".islower() True >>> ("i love you".upper()).isupper() True >>> "i love you".isupper() False
17、isdentifier()判断字符串是够包含该语言的保留字
>>> "def".isidentifier() True >>> "class".isidentifier() True
18、isprintable()判断字符串中所有字符串是否可打印,或字符串是空,都返回True,否则False
>>> "\n".isprintable() False >>> "a".isprintable() True >>> "".isprintable() True
19、isspace()判断字符串是否只包含空格、制表符和换行,且为非空。
>>> "".isspace() False >>> " ".isspace() True >>> "\n".isspace() True
20、istitle() 判断标题模式,以大写字母开头,后面均为小写的单词返回True,否则False
>>> "Abb Cdd".istitle() True >>> "abb Cdd".istitle() False
21、join()用指定字符将所有元素合并为一个新的字符串
>>> ",".join("12321") '1,2,3,2,1' >>> "aaaaa".join("12321") '1aaaaa2aaaaa3aaaaa2aaaaa1'
22、lower()转化字符串中所有大写为小写
>>> "ADGTH".lower() 'adgth' >>> ("ADGTH".lower().islower()) True
23、startwith()和endwith()如果字符串以该方法传入的字符串开始或结尾,则返回True,否则返回False
>>> "Hello world!".startswith("Hello") True >>> "Hello world!".endswith("world") False >>> "Hello world!".endswith("world!") True
24、split()使用该方法传入的字符分隔原字符串,可指定分隔次数,返回一个字符串列表,默认为空格。
rsplit(),与split作用相同,但是从右侧开始
>>> "i love you".split() ['i', 'love', 'you'] >>> "a1b1c1d1e".split("1") ['a', 'b', 'c', 'd', 'e'] >>> "a1b1c1d1e".split("1",3) ['a', 'b', 'c', 'd1e'] >>> "a1b1c1d1e".rsplit("1",3) ['a1b', 'c', 'd', 'e']
25、rjust()、ljust()和center()文本对齐方法
>>> "Hello".rjust(10) ' Hello' >>> "Hello".ljust(10) 'Hello ' >>> "Hello".center(10) ' Hello ' >>> "Hello".rjust(10,"-") '-----Hello' >>> "Hello".ljust(10,"-") 'Hello-----' >>> "Hello".center(10,"-") '--Hello---'
26、strip()、rstrip()和lstrip()删除空白字符。
>>> " hello World ".strip() 'hello World' >>> " hello World ".rstrip() ' hello World' >>> " hello World ".lstrip() 'hello World '
27、partition()在指定字符串中找str,如果找到则返回该字符前面部分
rpartition()与partition()类似,但从右边开始查找
>>> "aabbccddff".partition("c") ('aabb', 'c', 'cddff') >>> "aabbccddff".rpartition("c") ('aabbc', 'c', 'ddff')
28、replace()用第二参数字符串替换第一参数字符串,如果不指定次数,则全部替换
>>> "this is an apple".replace("a","A") 'this is An Apple' >>> "this is an apple".replace("a","A",1) 'this is An apple'
29、splitlines(),按换行符,对字符串进行分隔,如果没有指定keepends=True,则会将其从结果中移除
>>> "orange\napple".splitlines() ['orange', 'apple'] >>> "orange\napple".splitlines(keepends = True) ['orange\n', 'apple']
30、swapcase() 大小写互相转换
>>> "orange".swapcase() 'ORANGE' >>> "ABC".swapcase() 'abc'
31、zfill()这里Z值zero,用0将字符填充到指定长度
>>> "aaa".zfill(10) '0000000aaa'
相关文章推荐
- Python入门:字符串的分片与索引、字符串的方法
- Python入门_浅谈字符串的分片与索引、字符串的方法
- Python入门教程完整版(懂中文就能学会)视频\02python基础\day05\189-字符串-05-判断数字的三个方法.py
- 【零基础入门学习Python笔记014】字符串:各种奇葩的内置方法
- 浅谈python字符串方法的简单使用
- Python字符串基本方法介绍
- JavaScript字符串对象slice方法入门实例(用于字符串截取)
- Python学习笔记_字符串类型内建方法
- python3实现字符串的全排列的方法(无重复字符)
- 零基础学python-7.7 字符串格式化方法(2)
- python的三种字符串格式化方法
- Python实现针对给定字符串寻找最长非重复子串的方法
- Python 字符串内置方法
- Python字符串方法整理
- JavaScript字符串对象toUpperCase方法入门实例(用于把字母转换为大写)
- python字符串连接方法分析
- Python字符串解析方法汇总
- JavaScript link方法入门实例(给字符串加上超链接)
- Python字符串的encode与decode研究心得乱码问题解决方法
- python字符串格式化方法 format函数的使用