字符串常见方法(english)

本文介绍了Python中常用的字符串操作方法,包括查找子串、替换字符、分割字符串等实用技巧,并提供了具体的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

string normal method

find rfind

return first index or -1
mystr.find(str, start=0, end=len(mystr))

index rindex

Similar to find, if not in str there will raise a error
mystr.index(str, start=0, end=len(mystr))

count

return times
mystr.count(str, start=0, end=len(mystr))

replace

not more than count
mystr.replace(str1, str2, mystr.count(str1))

split

mystr.split(‘/’, maxsplit)

capitalize

upper first character of string
mystr.capitalize()

title()

upper fisrt character of every word in string
mystr.title()

startswith

return True or False
mystr.startswith(’s’)

endswith

return True or False
mystr.endswith(’s’)

lower()

lower every character in string
mystr.lower()

upper()

lower every character in string
mystr.lower()

ljust rjust center

in: ‘liuda’.ljust(10,’b’)
out: ‘liudabbbbb’

lstrip rstrip strip

in: ‘liudaliuda’.strip(‘liud’)
out: ‘aliuda’

partition rpatition

return tuple (before_str str behind_str)
in: ‘goodliudaliudagood’.partition(‘liuda’)
out: (‘good’, ‘liuda’, ‘liudagood’)
in: ‘goodliudaliudagood’.rpartition(‘liuda’)
out: (‘goodliuda’, ‘liuda’, ‘good’)

splitlines()

return a list of every line
mystr.splitlines()

isalpha() isdigit() isalnum() isspace()

alpha digit alpha or digit space

join

in: ‘-‘.join([‘my’, ‘name’, ‘is’, ‘liuda’])
out: ‘my-name-is-liuda’

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值