ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python(五)字符串的常用操作和编码、转码

2021-11-28 23:30:37  阅读:191  来源: 互联网

标签:编码 转码 python s2 s1 Python print True hello


字符串的驻留机制在这里插入图片描述

'''字符串的驻留机制'''
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'))  #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

在这里插入图片描述

字符串的大小写转换操作的方法

在这里插入图片描述

'''字符串的大小写转换的方法'''
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])  #从1开始截到5(不包含5),步长为1
print(s[::2])
print(s[::-1])
print(s[-6::1]) #从索引为-6开始,到最后一个元素结束,步长为1
ello
hloPto
nohtyP,olleh
Python

格式化字符串

在这里插入图片描述

在这里插入图片描述

#格式化字符串
#(1)%占位符
name='张三'
age=20
print('我叫%s,今年%d岁' % (name,age))

#(2) {}
print('我叫{0},今年{1}岁'.format(name,age))

#(3)f-string
print(f'我叫{name},今年{age}岁')

我叫张三,今年20岁
我叫张三,今年20岁
我叫张三,今年20岁
#########################################################################################
print('%10d' % 99)        #10表示的是宽度
print('%.3f' % 3.1415926) #.3表示是小数点后三位
#同时表示宽度和精度
print('%10.3f' % 3.1415926) #一共总宽度为10,小数点后3位
print('123456789')
        99
3.142
     3.142
123456789
print('{0:.3}'.format(3.1415926))   #.3表示的是一共是3位数
print('{0:.3f}'.format(3.1415926))  #.3f表示是3位小数
print('{:10.3f}'.format(3.1415926)) #同时设置宽度和精度,一共是10位,3位是小数
3.14
3.142
     3.142

字符串的编码转换

在这里插入图片描述

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='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'
天涯共此时
天涯共此时

小结

在这里插入图片描述

标签:编码,转码,python,s2,s1,Python,print,True,hello
来源: https://blog.csdn.net/weixin_48190863/article/details/121588499

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有