搜索
您的当前位置:首页正文

python编程如何删除字符

2024-08-01 来源:字库网

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)

执行结果:

推荐学习:《》

Top