文章目录
- 前言
- 一、字符串的驻留机制
- 二、字符串的常用操作
- 1.查询
- 2.字符串的大小写转换
- 3.字符串的对齐操作
- 4.字符串的劈分操作
- 5.字符串的判断方法
- 6.字符串的替换与合并
- 7.字符串的比较操作
- 8.字符串的切片操作
- 9.格式化字符串
- 10.字符串的编码与解码
- 总结
前言
python学习笔记 day4(仅供学习使用)
字符串:在python中字符串是基本数据类型,是一个不可变的字符序列。
一、字符串的驻留机制
a='Python'
b="Python"
c='''Python'''
print(a,id(a))
print(b,id(b))
print(c,id(c))
运行:
Python 140528244507440
Python 140528244507440
Python 140528244507440
说明:python字符串在内存中只有一份。
合法标识符:字母数字下划线。驻留机制的优缺点:
二、字符串的常用操作
1.查询
s='hello,hello'
print(s.index('lo')) #3
print(s.find('lo'))#3
print(s.rindex('lo')) #9
print(s.rfind('lo'))#9
#print(s.index('k')) #ValueError: substring not found
print(s.find('k')) #-1
#print(s.rindex('k')) #ValueError: substring not found
print(s.rfind('k')) #-1
运行:
3
3
9
9
-1
-1
2.字符串的大小写转换
s='hello,python'
a=s.upper() #转成大写之后,会产生一个新的字符串对象
print(a,id(a))
print(s,id(s))
b=s.lower() #转换之后,会产生一个新的字符串对象
print(b,id(b))
print(s,id(s))
print(b==s)
print(b is s) #False
s2='hello,Python'
print(s2.swapcase())
print(s2.title())
运行:
HELLO,PYTHON 140272587523696
hello,python 140272587523184
hello,python 140272587523824
hello,python 140272587523184
True
False
HELLO,pYTHON
Hello,Python
3.字符串的对齐操作
s='hello,Python'
'''居中对齐'''
print(s.center(20,'*'))
'''左对齐'''
print(s.ljust(20,'*'))
print(s.ljust(10))
print(s.ljust(20))
'''右对齐'''
print(s.rjust(20,'*'))
print(s.rjust(20))
print(s.rjust(10))
'''右对齐,使用0进行填充'''
print(s.zfill(20))
print(s.zfill(10))
print('-8910'.zfill(8))
运行:
****hello,Python****
hello,Python********
hello,Python
hello,Python
********hello,Python
hello,Python
hello,Python
00000000hello,Python
hello,Python
#0添加在符号之后,
-0008910
4.字符串的劈分操作
s='hello world Python'
lst=s.split()
print(lst)
s1='hello|world|Python'
print(s1.split(sep='|'))
print(s1.split(sep='|',maxsplit=1))
print('-------------------------------')
'''rsplit()从右侧开始劈分'''
print(s.rsplit())
print(s1.rsplit('|'))
print(s1.rsplit(sep='|',maxsplit=1))
运行:
['hello', 'world', 'Python']
['hello', 'world', 'Python']
['hello', 'world|Python']
-------------------------------
['hello', 'world', 'Python']
['hello', 'world', 'Python']
['hello|world', 'Python']
5.字符串的判断方法
s='hello,python'
print('1.',s.isidentifier()) #False
print('2.','hello'.isidentifier()) #True
print('3.','张三_'.isidentifier()) #True
print('4.','张三_123'.isidentifier()) #True
print('5.','\t'.isspace()) #True
print('6.','abc'.isalpha()) #True
print('7.','张三'.isalpha()) #True
print('8.','张三1'.isalpha()) #False
print('9.','123'.isdecimal()) #True
print('10.','123四'.isdecimal()) # False
print('11.','ⅡⅡⅡ'.isdecimal()) # False
print('12.','123'.isnumeric()) #True
print('13.','123四'.isnumeric())#True
print('14.','ⅡⅡⅡ'.isnumeric()) #True
print('15.','abc1'.isalnum()) #True
print('16.','张三123'.isalnum()) #True
print('17.','abc!'.isalnum()) #False
6.字符串的替换与合并
s='hello,Python'
print(s.replace('Python','Java'))
s1='hello,Python,Python,Python'
print(s1.replace('Python','Java',2))
lst=['hello','java','Python']
print('|'.join(lst))
print(''.join(lst))
t=('hello','Java','Python')
print(''.join(t))
print('*'.join('Python'))#字符串将对每个字符进行连接
运行:
hello,Java
hello,Java,Java,Python
hello|java|Python
hellojavaPython
helloJavaPython
P*y*t*h*o*n
7.字符串的比较操作
print('apple'>'app') #True
print('apple'>'banana') #False ,相当于97>98 >False
print(ord('a'),ord('b'))#97 98
print(ord('杨'))#26472
print(chr(97),chr(98))# a b
print(chr(26472))# 杨
'''== 与is的区别
== 比较的是 value
is 比较的是id是否相等'''
a=b='Python'
c='Python'
print(a==b) #True
print(b==c) #True
print(a is b) #True
print(a is c ) #True
print(id(a)) #2204259933168
print(id(b)) #2204259933168
print(id(c)) #2204259933168
8.字符串的切片操作
s='hello,Python'
s1=s[:5] #由于没有指定起始位置,所以从0开始切
s2=s[6:] #由于没有指定结束位置,所以切到字符串的最后一个元素
s3='!'
newstr=s1+s3+s2
print(s1)
print(s2)
print(newstr)
print('--------------------')
print(id(s))
print(id(s1))
print(id(s2))
print(id(s3))
print(id(newstr))
print('------------------切片[start:end:step]-------------------------')
print(s[1:5:1]) #从1开始截到5(不包含5),步长为1
print(s[::2]) #默认从0 开始,没有写结束,默认到字符串的最后一个元素 ,步长为2 ,两个元素之间的索引间隔为2
print(s[::-1]) #默认从字符串的最后一个元素开始,到字符串的第一个元素结束,因为步长为负数
print(s[-6::1]) # 从索引为-6开始,到字符串的最后一个元素结束,步长为1
运行:
hello
Python
hello!Python
--------------------
140231499072432
140231499073072
140231499073136
140231499072496
140231499073200
------------------切片[start:end:step]-------------------------
ello
hloPto
nohtyP,olleh
Python
9.格式化字符串
name='张三'
age=20
print('我叫%s,今年%d岁' % (name,age))
#(2) {}
print('我叫{0},今年{1}岁'.format(name,age))
#(3)f-string
print(f'我叫{name},今年{age}岁')#前面写个f表明格式化字符串。!!
运行:
我叫张三,今年20岁
我叫张三,今年20岁
我叫张三,今年20岁
例二:
print('%10d' % 99) #10表示的是宽度,即整个数字占据的位置,99前面八个空格,共十个位置。
print('%.3f' % 3.1415926) #.3表示是小数点后三位
#同时表示宽度和精度
print('%10.3f' % 3.1415926) #一共总宽度为10,小数点后 3位
print('hellohello')
运行:
99
3.142
3.142
hellohello
例三:
print('{0:.3}'.format(3.1415926)) #.3表示的是一共是3位数
print('{:.3f}'.format(3.1415926)) #.3f表示是3位小数#0可省略不写
print('{:10.3f}'.format(3.1415926)) #同时设置宽度和精度,一共是10位,3位是小数
运行:
3.14
3.142
3.142
10.字符串的编码与解码
s='你好呀,小周周'
#编码
print(s.encode(encoding='GBK')) #在GBK这种编码格中 一个中文占两个字节
print(s.encode(encoding='UTF-8')) #在UTF-8这种编辑格式中,一个中文占三个字节
#解码
#byte代表就是一个二进制数据(字节类型的数据)
byte=s.encode(encoding='GBK') #编码
print(byte.decode(encoding='GBK')) #解码
byte=s.encode(encoding='UTF-8')
print(byte.decode(encoding='UTF-8'))
运行:
b'\xc4\xe3\xba\xc3\xd1\xbd\xa3\xac\xd0\xa1\xd6\xdc\xd6\xdc'
b'\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x91\x80\xef\xbc\x8c\xe5\xb0\x8f\xe5\x91\xa8\xe5\x91\xa8'
你好呀,小周周
你好呀,小周周
总结