【马士兵】 Python基础--11(字符串)

Python基础–11

字符串的驻留机制

在这里插入图片描述

a='Python'
b='Python'
c='Python'
print('a:',a,id(a))
print('b:',b,id(b))
print('c:',c,id(c))
# a: Python 2834106576176
# b: Python 2834106576176
# c: Python 2834106576176

在这里插入图片描述

字符串查找

字符串查询操作方法

方法名称作用
index()查找子串substr第一次出现的位置,如果查找的子串不存在时,则抛出ValueError
rindex()查找子串substr最后一次出现的位置,如果查找的子串不存在时,则抛出ValueError
find()查找子串substr第一次出现的位置,如果查找的子串不存在时,则返回-1
rfind()查找子串substr最后一次出现的位置,如果查找的子串不存在时,则返回-1
s='hello,hello'
print(s.index('lo'))#3
print(s.rindex('lo'))#9
print(s.find('lo'))#3
print(s.rfind('lo'))#9
 
#print(s.index('k'))ValueError: substring not found
print(s.find('k')) #-1

字符串大小写转换

字符串的大小写转换方法

方法名称作用
upper()把字符串中所有字符都转换成大写字母
lower()把字符串中所有字符都转换成小写字母
swapcase()把字符串中所有大写字母都转换成小写字母,把所有小写字母都转换成大写字母
capitalize()把第一个字符转换成大写,把其余字符转换为小写
title()把每个单词的第一个字符转换为大写,把每个单词的剩余字符转换成小写

转换之后,会产生一个新的字符串对象

s='hello,python'
print(s,id(s))
print(s.upper(),id(s.upper()))
# hello,python 2564162926064
# HELLO,PYTHON 2564162925936
a='HELLO,PYTHON'
print(a,id(a))
print(a.lower(),id(a.lower()))
# HELLO,PYTHON 2564162926448
# hello,python 2564162926512
print(a==s.upper())
print(a is s.upper())
# True
# False
#内容相同,地址不同
 
s1='asfJSEOFH'
print(s1.swapcase())
print(s1.capitalize())
s2='sdjf ddjo EJOSD CNDDScjd'
print(s2.title())
# ASFjseofh
# Asfjseofh
# Sdjf Ddjo Ejosd Cnddscjd

字符串内容对齐

字符串内容对齐操作方法

在这里插入图片描述

宽度,指定填充符

s='hello,Python'
print(s.center(20,'*'))
# ****hello,Python****
print(s.ljust(20,'*'))
print(s.ljust(10,'*'))
print(s.ljust(20))
# hello,Python********
# hello,Python
# hello,Python
print(s.rjust(20,'*'))
print(s.rjust(20))
print(s.rjust(10))
# ********hello,Python
#         hello,Python
# hello,Python
print(s.zfill(20))
print('-9010'.zfill(8))
# 00000000hello,Python
# -0009010

字符串的劈分

字符串劈分操作的方法

在这里插入图片描述

s='hello world python'
lst=s.split()
print(lst)
s1='hello|world|python'
print(s1.split())
print(s1.split(sep='|'))#让分隔符为竖线
print(s1.split(sep='|',maxsplit=1))#只分一次
# ['hello', 'world', 'python']
# ['hello|world|python']
# ['hello', 'world', 'python']
# ['hello', 'world|python']
 
s='hello world python'
lst=s.rsplit()
print(lst)
s1='hello|world|python'
print(s1.rsplit())
print(s1.rsplit(sep='|'))#让分隔符为竖线
print(s1.rsplit(sep='|',maxsplit=1)) #从右侧开始劈分,只分一次
# ['hello', 'world', 'python']
# ['hello|world|python']
# ['hello', 'world', 'python']
# ['hello|world', 'python']

字符串判断

判断字符串操作的方法

在这里插入图片描述

s='hello,python'
print(s.isidentifier())
s='hello2341_python'#字母数字下划线
print(s.isidentifier())
# False
# True
s=' \n \t'
print(s.isspace())
# True
s='dsjfSDLFJ'
print(s.isalpha())
s='dsf3'
print(s.isalpha())
# True
# False
s='238509'
print(s.isdecimal())
s='九02347'
print(s.isdecimal())
# True
# False
s='32840四五十四'
print(s.isnumeric())
# True
s='sdjf324'
print(s.isalnum())
# True

字符串替换和合并

在这里插入图片描述

s='hello,python,python,python'
print(s.replace('python','c++'))
s='hello,python,python,python'
print(s.replace('python','c++',2))
# hello,c++,c++,c++
# hello,c++,c++,python
 
lst=['hello','java','python']
print('|'.join(lst))
print(''.join(lst))
# hello|java|python
# hellojavapython
t=('hello','java','python')
print('|'.join(t))
print(''.join(t))
# hello|java|python
# hellojavapython
print('*'.join('Python'))
# P*y*t*h*o*n
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

科研达人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值