首页 > IT职业 > IT图书 > python编程如何删除字符

Python中可以使用replace()方法删除指定字符。
Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
replace()方法语法:
str.replace(old, new[, max])
参数
old -- 将被替换的子字符串。
new -- 新字符串,用于替换old子字符串。
max -- 可选字符串, 替换不超过 max 次
示例:
s1 = input('s1:')
s2 = input('s2:')
for i in s1:
if i in s2:
s1 = s1.replace(i,'')
print(s1)
执行结果:


推荐学习:《Python教程》
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/933/viewspace-2833966/ ,如需转载,请注明出处,否则将追究法律责任。