【python基础】python中常用字符串函数详解
文章目录
1 字符串查询(index,find)
- 字符串大小写转换操作(upper、lower、swapcase、capitalize和title)
- 字符串对齐(center,just和zfill)
- 分割字符串(split、splitlines和partition)
- 合并与替换(join,replace)
- 判断字符串(isidentifier、isspace、isalpha、isdecimal、isnumeric和isalnum等)
- 字符串的比较(,max,min等)
- 去除两端多余字符操作(strip)
- 判断开头结尾字符串(startswith,endswith)
- 字符串计数(count,len)
- 字符串的编码与解码(encode,decode)
1 字符串查询(index,find)
建议使用find,因为如果没有找到匹配的字符串,index方法会报异常。
代码举例
str1 = "my name is qlee,what your name?"
str2 = "name"
print(str1.find(str2))#全部查找
print(str1.find(str2,5))#从第5个元素开始查找
print(str1.find(str2,35))# 从第35个元素开始查找,超过元素索引或者没找到,不会报错
输出:
3
26
-1
- 字符串大小写转换操作(upper、lower、swapcase、capitalize和title)
代码举例:
txt = "my name is Qlee"
print(txt.upper())
print(txt.lower())
print(txt.swapcase())
print(txt.capitalize())
print(txt.title())
输出:
MY NAME IS QLEE
my name is qlee
MY NAME IS qLEE
My name is qlee
My Name Is Qlee
- 字符串对齐(center,just和zfill)
代码举例:
str = "hello world!"
print (str.center(30, ''))
print (str.ljust(30, ''))
print (str.rjust(30, '*'))
print (str.zfill(30))
输出:
hello world!
hello world!**
**hello world!
000000000000000000hello world!
- 分割字符串(split、splitlines和partition)
代码举例:
str = "my name is qlee, what is your name"
print(str.split()) # 以空格为分隔符
print(str.split('i',1)) # 以 i 为分隔符
print(str.split('b')) # 以b为分隔符,没找到不会报错
print(str.partition("name"))#找到第一个name,分割为三部分
print(str.rpartition("name"))#反向找到第一个name,分割为三部分
str = """my name is qlee
what is your name"""
print(str.splitlines())
输出:
['my', 'name', 'is', 'qlee,', 'what', 'is', 'your', 'name']
['my name ', 's qlee, what is your name']
['my name is qlee, what is your name']
('my ', 'name', ' is qlee, what is your name')
('my name is qlee, what is your ', 'name', '')
['my name is qlee', ' what is your name']
- 合并与替换(join,replace)
代码举例:
print("----------join-----------")
seq1 = ("h", "e", "l", "l", "o") #元组
seq2 = ["h", "e", "l", "l", "o"] #列表
seq3 = "hello" #字符串
print ("".join(seq1)) #无分隔符
print (" ".join(seq1))#空格
print (",".join(seq1))#","
print("----------replace-----------")
str1 = "my name is qlee"
print (str1.replace("qlee", "lq")) #注意str1本身不发生改变
输出:
----------join-----------
hello
h e l l o
h,e,l,l,o
----------replace-----------
my name is lq
- 判断字符串(isidentifier、isspace、isalpha、isdecimal、isnumeric和isalnum等)
举例说明:
print("hello&".isidentifier())#False,&为非法标识符
print(" t".isspace())#False,"t"为非空
print("aldflafd你好".isalpha())#ture,中文也可以
print("123四".isdecimal())#False,中文不属于十进制
print("123四".isnumeric())#True,中文、罗马字符的数字也算
print("123abc".isalnum())#True,只能字母和数字
print("123四".isdigit())#False,不能包括中文
print("".islower())# False,不能为空字符
print("TLUHBH".isupper())#True
print("My Name Is Qlee".istitle())#True,只有第一个字符为大写
print("我是中国人".isascii())#False,中文不属于ascii
print("Hello!\nAre you ?".isprintable()) #False,\n不可打印
- 字符串的比较(<,>,max,min等)
字符串的比较操作:
运算符:> , >=, <, <=, ==, !=
比较规则:从第一个以此往下比较。
比较原理:比较的是oridinal value(原始值)
举例说明:
str = "mynameisqlee"
print("max(str): ", max(str),"min(str): ", min(str))
print("hello" < "Hello")
print(ord("c"))
print(chr(98))
输出:
max(str): y min(str): a
False
99
b
- 去除两端多余字符操作(strip)
代码举例:
str = " my name is qleeeeee"
print(()) #去掉两边的空白
print(("e")) #去掉右边的"e"字符
str = "!!!!my name is qlee!!!!"
print(("!")) #去掉左右边的"!"字符
输出:
my name is qleeeeee
my name is ql
my name is qlee
- 判断开头结尾字符串(startswith,endswith)
代码距离:
str = "my name is qlee"
print(("my"))#True
print(("is"))#False
- 字符串计数(count,len)
代码举例:
str = "my name is qlee, what is your name?"
print(("name")) #2
print(len(str))# 35
- 字符串的编码与解码(encode,decode)
代码举例:
str = "我是中国人"
str_utf8 = ("UTF-8")
str_gbk = ("GBK")
print("原字符串:",str)
print("------------编码---------------")
print("UTF-8 编码:", str_utf8)
print("GBK 编码:", str_gbk)
print("------------解码---------------")
print("UTF-8 解码:", str_utf8.decode('UTF-8', 'strict'))
print("GBK 解码:", str_gbk.decode('GBK', 'strict'))
输出:
原字符串: 我是中国人
------------编码---------------
UTF-8 编码: b'\xe6\x88\x91\xe6\x98\xaf\xe4\xb8\xad\xe5\x9b\xbd\xe4\xba\xba'
GBK 编码: b'\xce\xd2\xca\xc7\xd6\xd0\xb9\xfa\xc8\xcb'
------------解码---------------
UTF-8 解码: 我是中国人
GBK 解码: 我是中国人