py:字符串操作方法

字符串方法

#!coding=utf-8#
'''
eval(str)
将字符串当成有效的表达式,并返回结果,相当与int
'''
num1=eval('123')
print(num1)
print(type(num1))
print(eval('12-3'))
print(eval('-123'))
#len(str)
#返回字符串的长度
print(len('Tom is a good man!'))
print(len('Tom is 一 good man!'))
'''
lower(str):将str中的字母转换成小写字母
'''
str20='Tom is a good man'
str21=str20.lower()
print('str20=%s,str21=%s' %(str20,str21))
#upper()转换字符串中的小写为大写字母
str21='TOM is a good man'
print('的大写是:',str21.upper())
print('我是')
#swapcase():小写转成大写,大写转成小写
str22='Tom Is A Good Man'
print(str22.swapcase())
#capitalize:首字母大写,其他小写
str21='TOM is a good man'
print(str21.capitalize())
#title()每个单词的首字母大写,其他小写
str21='TOM Is A Good Man'
print(str21.title())
#center(width,fillchar):长度width,字符居中,其余的填充fillchar字符
str21='TOM Is A Good Man'
print(str21.center(30,"-"))
#ljust(width,[fillchar]):长度width,字符居左,其余的填充fillchar字符
str21='TOM Is A Good Man'
print(str21.ljust(30,'-'))
#rjust(width,[fillchar]):长度width,字符居右,其余的填充fillchar字符
str21='TOM Is A Good Man'
print(str21.rjust(30,'-'))
#zfill(width):长度width,字符居右,其余的填充0
str21='TOM Is A Good Man'
print(str21.zfill(30))
#count(str1,[stat],[end]):找个数
str21='TOM Is A Good Good Man'
print(str21.count('o'))  #4个
print(str21.count('oo'))  #2个
print(str21.count('Good'))  #2个

#find(str1,[stat],[end]):找位置,没有返回-1
str21='TOM Is A Good Good Man'
print(str21.find('oo'))  #10

#rfind(str1,[stat],[end]):找位置,没有返回-1
str21='TOM Is A Good Good Man'
print(str21.rfind('oo'))  #15

#index(str1,[stat],[end]):找位置,没有返回异常
str21='TOM Is A Good Good Man'
print(str21.index('oo'))  #10
#rindex(str1,[stat],[end]):找位置,没有返回异常
str21='TOM Is A Good Good Man'
print(str21.rindex('oo'))  #15

#lstrip(fillchar):截掉左侧的fillchar,默认为空格
str21='****TOM Is A Good Good Man'
print(str21.lstrip('*'))
#rstrip(fillchar):截掉右侧的fillchar,默认为空格
str21='****TOM Is A Good Good Man   '
print(str21.rstrip())


#lstrip(fillchar):截掉两侧的fillchar,默认为空格
str21='****TOM Is* A Good Good Man****'
print(str21.strip('*'))
print(str21.strip())


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值