字符串的基本操作

"""
字符串的大小写转换操作
1.upper()把所有字符转换成大写字母
2.lower()把字符串中所有字符都转换成小写字母
3.swapcase()把字符串中所有大写字母转换成小写字母,所有小写字母转换成大写字母
4.capitalize()把第一个字符转换成大写,其余字符转换成小写
5.title()每一个单词的第一个字符转换成大写,剩余字符转换为小写
"""
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(s==b)#True
print(s is b)#False

s2='hello,Python'
print(s2.swapcase())#HELLO,pYTHON
print(s2.title())#Hello,Python


"""
字符串内容对齐操作的方法
1.center()居中对齐
2.ljust()左对齐
3.rjust()右对齐
4.zfill()右对齐
"""
#居中对齐
s='hello,Python'
print(s.center(20,'*'))#****hello,Python****
#左对齐
#无填充指定,按照空来算
print(s.ljust((20)))#hello,Python
print(s.ljust(20,'*'))#hello,Python********

#右对齐
print(s.rjust(20,'&'))#&&&&&&&&hello,Python
print(s.rjust(20))#        hello,Python

#右对齐使用0进行填充
print(s.zfill(20))#00000000hello,Python
print(s.zfill(10))#hello,Python
print('-9870'.zfill(8))#-0009870
"""
字符串劈分操作的方法
1.split()从左边开始劈分
2.rsplit()从右边开始劈分
"""
#没有指定分隔符,就按照空格进行分割
s='hello python world'
lst=s.split()
print(lst)#['hello', 'python', 'world']

#指定具体的分割符,按照分隔符进行分割
s1='hello|python|world'
lst=s1.split(sep='|')
print(lst)#['hello', 'python', 'world']

print(s1.split(sep='|',maxsplit=1))#['hello', 'python|world']

print('-----从右侧开始劈分-----')
print(s.rsplit())#['hello', 'python', 'world']
print(s1.rsplit(sep='|'))#['hello', 'python', 'world']
print(s1.rsplit(sep='|',maxsplit=1))#['hello|python', 'world']
"""
字符串判断的相关方法
1.isidentifier()判断指定的字符串是不是合法的标识符
2.isspace()判断指定的字符串是否由空白字符串组成(回车、换行、水平制表符)
3.isalpha()判断指定的字符串是否全部由字母组成
4.isdecimal()判断指定字符串是否全部由十进制数字组成
5.isnumeric()判断指定的字符串是否全部由数字组成
6.isallnum()判断指定的字符串是否全部由指定的数字和字母组成
"""
print('----isidentifier()----')
s='hello,python'
print('1',s.isidentifier())#1 False
print('2','hello'.isidentifier())#2 True
print('3','张三_'.isidentifier())#3 True
print('4','张三_123'.isidentifier())#4 True

print('----issapce()-------')
print('5','\t'.isspace())#5 True

print('6','abc'.isalpha())#6 True
print('7','张三'.isalpha())#7 True
print('8','张三1'.isalpha())#8 False

print('9','123'.isdecimal())#9 True
print('10','123四'.isdecimal())#10 False
print('11','Ⅰ'.isdecimal())#11 False

print('12','123'.isnumeric())#12 True
print('13','123四'.isnumeric())#13 True
print('14','Ⅰ'.isnumeric())#14 True

print('15','abc1'.isalnum())#15 True
print('16','张三123'.isalnum())#16 True
print('17','abc!'.isalnum())#17 False


"""
字符串操作的其他方法
1.字符串的替换
    replace()
    第一个参数指定被替换的子串
    第二个参数指定替换子串的字符串
    第三个参数指定最大替换次数
2.字符串的合并
    join()
    将列表或元组的字符串合并成一个字符串
"""
#使用replace()
s='hello,python'
print(s.replace('python','java'))#hello,java
s1='hello,python,python,python'
print(s1.replace('python','java',2))#hello,java,java,python

#join()
lst=['hello','python','world']
print('|'.join(lst))#hello|python|world
print(''.join(lst))#hellopythonworld

t=('hello','world','python')
print('.'.join(t))#hello.world.python
print(''.join(t))#helloworldpython

print('*'.join('python'))#p*y*t*h*o*n
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值