python之字符串替换

2023-11-03

方法一:

"""
str.replace(old, new[, max])

old -- 将被替换的子字符串。
new -- 新字符串,用于替换old子字符串。
max -- 可选字符串, 替换不超过 max 次
"""

temp_str = 'this is a test'
print(temp_str.replace(temp_str[2],'aa'))
print(temp_str.replace(temp_str[:2],'aa'))

>>>
thaas aas a test
aais is a test

方式二:

在字符串中替换自己想要的字符串

# -*- coding=utf-8 -*-
s = "{a}bc"
s=s.format(a="123")
print(s)

>>>123bc
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python之字符串替换 的相关文章

随机推荐