文章目录
1、字符串的驻留机制
#驻留机制指对再次创建的相同的字符串,不会开辟新的空间,而是把已有的字符串的地址赋给新的变量,这几个字符串变量共用一个地址。
a = 'python'
b = "python"
c = '''python'''
print(a,id(a)) #输出:python 1874508798960
print(b,id(b)) #输出:python 1874508798960
print(c,id(c)) #输出:python 1874508798960
驻留机制的满足情况:
- 字符串长度为0或1时。
- 符合标识符的字符串。
- 字符串只在编译时进行驻留,而非运行时。
- [-5,256]之间的整数数字。
#强制驻留
import sys
a = 'abc%'
b = "abc%"
a = sys.intern(b)
print(a is b) #输出:True
2、查找字符串中的子串
#查找子串第一次出现的位置,不存在则报错
s = "hello,hello"
print(s.index('lo')) #输出:3
print(s.index('k')) #报错
#查找子串最后一次出现的位置,不存在则报错
s = "hello,hello"
print(s.rindex('lo')) #输出:9
print(s.rindex('k')) #报错
#查找子串第一次出现的位置,不存在则返回-1
s = "hello,hello"
print(s.find('lo')) #输出:3
print(s.find('k')) #输出:-1
#查找子串最后一次出现的位置,不存在则返回-1
s = "hello,hello"
print(s.rfind('lo')) #输出:9
print(s.rfind('k')) #输出:-1
3、字符串中大小写转换操作
s = "hello world"
#全部大写,生成另一个字符串
a = s.upper()
print(s,id(s)) #输出:hello,python 2134125876016
print(a,id(a)) #输出:HELLO,PYTHON 2133868981488
#全部小写,生成另一个字符串
b = s.lower()
print(s,id(s)) #输出:hello,python 2134125876016
print(b,id(b)) #输出:hello,python 2134338769054
#字符串中的大写变小写,小写变大写
s2 = "Hello,World"
print(s2.swapcase()) #输出:hELLO,wORLD
#首字母变成大写,其他的变小写
s3 = "hElLo,wORld"
print(s3.capitalize()) #输出:Hello,world
#每个单词的首字母变成大写,其他的变小写
print(s3.title()) #输出:Hello,World
4、字符串中内容对齐操作
s = "hello,Python"
#居中对齐
print(s.center(20,"*")) #输出:****hello,Python****
#左对齐
print(s.ljust(20,"*")) #输出:hello,Python********
print(s.ljust(20)) #输出:hello,Python ,填充符不写则默认为空格
print(s.ljust(5)) #输出:hello,Python,长度不够直接输出原字符串
#右对齐
print(s.rjust(20,"*")) #输出:********hello,Python
#右对齐,且左边用0填充,且参数只有位数而没有填充字符
print(s.zfill(20)) #输出:00000000hello,Python
print("-8910",zfill(8)) #输出:-0008910,加上减号一共8位
5、字符串的分割操作
#默认从空格分割
s1 = "hello world Python"
lst1 = s1.split()
print(lst1) #输出:['hello','world','Python']
#自己设定分割符
s2 = "hello|world|Python"
lst2 = s2.split(sep = '|')
print(lst2) #输出:['hello','world','Python']
#设定最大分割次数,最后剩下的单独在一块
lst3 = s2.split(sep = '|',maxsplit = 1)
print(lst3) #输出:['hello','world|Python']
#从右侧开始分割:rsplit()
lst4 = s1.rsplit()
print(lst4) #输出:['hello','world','Python']
lst5 = s2.rsplit(sep = '|',maxsplit = 1)
print(lst5) #输出:['hello|world','Python']
6、判断字符串组成成分的操作
#判断字符串是否为合法的标识符
s = "hello,python"
print(s.isidentifier()) #输出:False,合法的字符串只包含字母、数字、文字、下划线,不包含其他符号。
print("hello".isidentifier()) #输出:True
print("张三_".isidentifier()) #输出:True
#判断字符串是否为空白字符串
print("\t".isspace()) #输出:True
#判断字符串是否全部由字母或文字组成
print("abc".isalpha()) #输出:True
print("张三".isalpha()) #输出:True
print("こんにちは".isalpha()) #输出:True
#判断字符串是否全部由十进制数字组成
print("123".isdecimal()) #输出:True
print("123四".isdecimal()) #输出:False
#判断字符串是否全部由数字组成
print("123".isnumeric()) #输出:True
print("123四".isnumeric()) #输出:True
#判断字符串是否全部由字母和文字和数字组成
print("abc123".isalnum()) #输出:True
print("张三ABC123".isalnum()) #输出:True
7、字符串的替换与合并操作
#字符串的替换
s1 = "hello,python"
print(s1.replace("python","world")) #输出:hello,world
s2 = "hello,python,python,python"
print(s2.replace("python","world",2)) #输出:hello,world,world,python
#列表或元组合并成字符串
lst = ["hello","world","python"]
print("|".join(lst)) #输出:hello|world|python
print("".join(lst)) #输出:helloworldpython
print(" ".join(lst)) #输出:hello world python
t = ("hello","world","python")
print("|".join(t)) #输出:hello|world|python
print("*".join("python")) #输出:p*y*t*h*o*n
8、字符串的比较操作
print("apple">"app") #输出:True
print(ord("a"),ord("b")) #输出:97 98,ord返回一个字符的ASCII码
print("apple">"ppl") #输出:False
print(ord("a"),ord("p")) #输出:97 112
print(ord("好")) #输出:22909
print(chr(22909)) #输出:好
#==比较的是value,is比较的是id
a = b = "python"
print(a == b) #输出:True
print(a is b) #输出:True
9、字符串的切片操作
#不像列表那样,字符串是不可变类型,不能增删改。并且切片后均产生新的对象。
s = "hello,python"
s1 = s[:5] #由于没有指定起始位置,从0开始
print(s1) #输出:hello
s2 = s[6:] #由于没有指定结束位置,切到最后
print(s2) #输出:python
s3 = "!"
newstr = s1+s3+s2
print(newstr) #输出:hello!python
print(s[::2]) #输出:hlopto
print(s[::-1]) #输出:nohtyp,olleh
print(s[-6:]) #输出:python
10、格式化字符串
#%作为占位符
name = "张三"
age = 21
print("我叫%s,今年%d岁了。" % (name,age)) #输出:我叫张三,今年21岁了。
#{}作为占位符
name = "张三"
age = 21
print("我叫{0},今年{1}岁了,我们班有好多人都叫{0}。".format(name,age)) #输出:我叫张三,今年21岁了,我们班有好多人都叫张三。
#f-string
name = "张三"
age = 21
print(f"我叫{name},今年{age}岁了。") #输出:我叫张三,今年21岁了。
print("%10d" % 99) #输出: 99,10表示的是宽度。
print("%.3f" % 3.1415926) #输出:3.142,.3表示小数点后三位。
print("%10.3f" % 3.1415926) #输出: 3.142,同时表示宽度和精度。
print("{0:.3}".format(3.1415926)) #输出:3.14
print("{0:.3f}".format(3.1415926)) #输出:3.142
print("{0:10.3f}".format(3.1415926)) #输出: 3.142