aStr = input("Please input: ")
if "A" in aStr:
print("Your output is: " + aStr.replace("A","@@@@"))
elif "a" in aStr:
print("Your output is: " + aStr.replace("a","@@@@"))
>>> x = 'Hello world'
>>> x.replace('l', '@')
'He@@o wor@d'
>>> help(str.replace)
Help on method_descriptor:
replace(...)
S.replace(old, new[, count]) -> str
Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
string.replace()
在python3.x中不推荐使用,因此可以使用str.replace()
或re.sub()
使用
replace
比使用regex更简单,对于大多数简单的使用场景来说都很好。你知道吗如果要更改大写或小写A/A
一个
string
对象有一个replace
方法。你可以这样做:您还可以通过索引访问单个字符,如
x[0]
,但请注意,字符串在Python中是不可变的,因此您不能为字符赋值。你知道吗编辑:每隔出现一次就替换一次有点棘手,目前我想不出一个好的一行,但是这个详细和稍微不pythonic的函数可以:
相关问题 更多 >
编程相关推荐