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
3
3
9
9

 

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

#将字符串转换成大写
s='hello,python'
a=s.upper()        #转成大写之后,会产生一个新的字符串对象
print(a,id(a))
print(s,id(s))

#将字符串转换成小写
s1='BY,PYTHON'
b=s1.lower()
print(b,id(b))

#将字符串中所有大写字母转换成小写字母,把所有小写字母都转换成大写字母
s2='hello,Python'
c=s2.swapcase()
print(c)
#将每个单词的首字母变成大写
d=s2.title()
print(d)
#将第一个字符转换为大写
n=s2.capitalize()
print(n)
HELLO,PYTHON 2014377983024
hello,python 2014377982768
by,python 2014377983152
HELLO,pYTHON
Hello,Python
Hello,python

 

3、字符转内容对齐操作的方法

s='hello,Python'
print(s.center(20,'*'))    #居中对齐

print(s.ljust(20,'*'))     #左对齐
print(s.ljust(10))

print(s.rjust(20,'*'))     #右对齐
print(s.rjust(20))
print(s.rjust(10))

print(s.zfill(20))         #右对齐,使用0进行填充
print(s.zfill(10))
print('-8910'.zfill(8))
****hello,Python****
hello,Python********
hello,Python
********hello,Python
        hello,Python
hello,Python
00000000hello,Python
hello,Python
-0008910

 

4、字符串劈分操作的方法

s='hello world Python'
lst=s.split()              #从左侧开始分割
print(lst)
print('----------------')
s1='hello|world|Python'
print(s1.split(sep='|'))
print(s1.split(sep='|',maxsplit=1))
print('-----------------')
print(s.rsplit())           #从右侧开始分割
print(s1.rsplit(sep='|'))
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、判断字符串操作的方法

6、字符串其他操作的方法

s='hello,Python'
print(s.replace('Python','Java'))
s1='hello,Python,Python,Python'
print(s1.replace('Python','Java',2))
print('---------------------------')
lst=['hello','java','Python']
print('|'.join(lst))
print(''.join(lst))
print('---------------------------')
t=('hello','java','Python')
print(''.join(t))
print('-----------------------------')
print('*'.join('Python'))
hello,Java
hello,Java,Java,Python
---------------------------
hello|java|Python
hellojavaPython
---------------------------
hellojavaPython
-----------------------------
P*y*t*h*o*n

 

7、字符串的比较操作

'''==与is的区别: == 比价的是 value,is  比较的是id是否相等'''
a=b='Python'
c='Python'
print(a==b)
print(b==c)

print(a is b)
print(a is c)
True
True
True
True

 

8、字符串的切片操作

s='hello,Python'
s1=s[:5]
s2=s[6:]
s3='!'
new_str=s1+s3+s2

print(s1)
print(s2)
print(new_str)

print(('------------切片[start:end:stop]---------------'))
print(s[1:5:1])      #从1开始截到5(不包含5),步长为1
print(s[::2])        #默认从0开始,没有写结束,默认到字符串的最后一个元素,步长为2
print(s[::-1])       #默认从字符串的最后一个元素开始,到字符串的第一个元素结束,因为步长为负数
hello
Python
hello!Python
------------切片[start:end:stop]---------------
ello
hloPto
nohtyP,olleh

 

 

9、格式化字符串

 

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

print('我叫{0},今年{1}岁'.format(name,age))   #用 {} 格式化字符串

print(f'我叫{name},今年{age}岁')      #用f-string格式化字符串

我叫张三,今年20岁
我叫张三,今年20岁
我叫张三,今年20岁

 

print('%10d' % 99)     #10表示的是宽度
print('%.3f' % 3.1415926)    #.3表示是小数点后三位
print('%10.3f' % 3.1415926)  #同时表示宽度和精度

print('{}'.format(3.1415926))
print('{0:.3}'.format(3.1415926))    #.3表示的是一共是3位数
print('{:.3f}'.format(3.1415926))    #.3f表示是3位小数,0可省略
print('{:10.3f}'.format(3.1415926))  #同时设置宽度和精度
        99
3.142
     3.142
3.1415926
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'\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'
天涯共此时
天涯共此时

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值