Python中字符串处理方法例子

本文详细介绍Python中字符串的八种常用操作方法,包括转换大小写、居中填充、计数子串、连接迭代器元素、替换子串、分割子串及去除指定字符。通过实例展示如何运用这些方法进行高效字符串处理。

共八种方法 method()

#<str>.<method>.py
str = "Great Day"
#str.lower()/str.upper()
print("1.{}".format(str.lower()))
print("2.{}".format(str.upper()))
#str.center(width[,fillchar])以str为中心,两侧填充char
print("8.{}".format(str.center(20,"=")))
#str.count(sub)返回子串sub出现的次数
print("3.{}".format(str.count("a")))
#str.join(iter)在iter元素间插入str
print("7.{}".format(" ".join(str)))
#str.replace(old,new)用新字符串new代替str的旧子串
print("6.{}".format(str.replace("ay","og")))
#str.split(sep=None)以子串为分割点放回序列
print("4.{}".format(str.split(sep=None)))
print("4(1).{}".format(str.split(sep="ea")))
#str.strip(chars)从str中去除chars左侧或右侧含有的字符
print("5.{}".format(str.strip("G Dy")))

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值