字符串的驻留机制
'''字符串的驻留机制'''
a='python'
b="python"
c='''python'''
print(a,id(a))
print(b,id(b))
print(c,id(c))
python 1368352737136
python 1368352737136
python 1368352737136
s1=''
s2=''
print(s1 is s2)
s1='%'
s2='%'
print(s1 is s2)
s1='abcx'
s2='abcx'
print(s1 is s2)
print(id(s1))
print(id(s2))
s1="abc#%%"
s2="abc#%%"
print(s1 is s2)
print(id(s1))
print(id(s2))
True
True
True
2427781498800
2427781498800
True
2427781498224
2427781498224
字符串的常用操作
字符串的查询操作的方法
'''字符串的查询操作'''
s='hello,hello'
print(s.index('lo'))
print(s.find('lo'))
print(s.rindex('lo'))
print(s.rfind('lo'))
print(s.find('k'))
print(s.rfind('k'))
字符串的大小写转换操作的方法
'''字符串的大小写转换的方法'''
s='hello,python'
a=s.upper()
print(a,id(a))
print(s,id(s))
print("-----------转换成小写-------------")
b=s.lower()
print(b,id(b))
print(s,id(s))
HELLO,PYTHON 2021884151728
hello,python 2021884151216
-----------转换成小写-------------
hello,python 2021884152048
hello,python 2021884151216
s2='hello,Python'
print(s2.swapcase())
print(s2.title())
HELLO,pYTHON
Hello,Python
字符串内容对齐操作
s='hello,Python'
'''居中对齐'''
print(s.center(20,'*'))
'''左对齐'''
print(s.ljust(20,'*'))
print(s.ljust(10))
print(s.ljust(20))
****hello,Python****
hello,Python********
hello,Python
hello,Python
'''右对齐'''
print(s.rjust(20,'*'))
'''右对齐,使用0进行填充'''
print(s.zfill(20))
print('-8977'.zfill(8))
********hello,Python
00000000hello,Python
-0008977
字符串劈分操作
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']
判断字符串操作
s='hello,python'
print('1',s.isidentifier())
print('2','张三_123'.isidentifier())
print('3','\t'.isspace())
print('4','abc'.isalpha())
print('5','张三1'.isalpha())
print('6','123'.isdecimal())
print('7','123四'.isnumeric())
print('8','张三123'.isalnum())
print('9','张三!'.isalnum())
1 False
2 True
3 True
4 True
5 False
6 True
7 True
8 True
9 False
字符串的替换和合并
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))
hello,Java
hello,Java,Java,Python
hello|Java|Python
helloJavaPython
t=('hello','Java','Python')
print(''.join(t))
print('*'.join('Python'))
helloJavaPython
P*y*t*h*o*n
字符串的比较操作
print('apple'>'app')
print('apple'>'banana')
print(ord('a'),ord('b'))
print(ord('赵'))
print(chr(97),chr(98))
print(chr(36213))
True
False
97 98
36213
a b
赵
'''==与is的区别
==比较的是value
is 比较的是id是否相等'''
a=b='Python'
c='Python'
print(a==b)
print(b==c)
print(a is b)
print(a is c)
print(id(a))
print(id(b))
print(id(c))
True
True
True
True
2162432169648
2162432169648
2162432169648
s='hello,Python'
s1=s[:5]
s2=s[6:]
s3='!'
new=s1+s3+s2
print(s1)
print(s2)
print(new)
hello
Python
hello!Python
print('----------切片[start:end:step]--------------------')
s='hello,Python'
print(s[1:5:1])
print(s[::2])
print(s[::-1])
print(s[-6::1])
ello
hloPto
nohtyP,olleh
Python
格式化字符串
name='张三'
age=20
print('我叫%s,今年%d岁' % (name,age))
print('我叫{0},今年{1}岁'.format(name,age))
print(f'我叫{name},今年{age}岁')
我叫张三,今年20岁
我叫张三,今年20岁
我叫张三,今年20岁
print('%10d' % 99)
print('%.3f' % 3.1415926)
print('%10.3f' % 3.1415926)
print('123456789')
99
3.142
3.142
123456789
print('{0:.3}'.format(3.1415926))
print('{0:.3f}'.format(3.1415926))
print('{:10.3f}'.format(3.1415926))
3.14
3.142
3.142
字符串的编码转换
s='天涯共此时'
print(s.encode(encoding='GBK'))
print(s.encode(encoding='UTF-8'))
byte=s.encode(encoding='GBK')
print(byte.decode(encoding='GBK'))
byte=s.encode(encoding='UTF8')
print(byte.decode(encoding='UTF-8'))
b'\xcc\xec\xd1\xc4\xb9\xb2\xb4\xcb\xca\xb1'
b'\xe5\xa4\xa9\xe6\xb6\xaf\xe5\x85\xb1\xe6\xad\xa4\xe6\x97\xb6'
天涯共此时
天涯共此时
小结
|