如何在 python 中使用 for 循环替换字符串的字符?
How to replace a character of a string using for loop in python?
我正在尝试编写一个代码来查找特殊字符并将它们替换为 *
。
例如:
L!ve l@ugh l%ve
这应该更改为
L*ve l*ugh l*ve
这是我到目前为止尝试过的
a = input()
spe = "+=/_(*&^%$#@!-.?)"
for i in a:
if i in spe:
b = a.replace(i,"*")
else:
b = i
print(b,end="")
这个returns像这样
Lve lugh lve
为什么我会变成这样?
以及如何解决这个问题?
您正在尝试修改整个字符串,而您应该只修改字符。
修改您的代码,这将是:
a = '(L!ve l@ugh l%ve)'
spe = set("+=/_(*&^%$#@!-.?)") # using a set for efficiency
for char in a:
if char in spe:
print('*', end='')
else:
print(char, end='')
输出:*L*ve l*ugh l*ve*
更像 pythonic 的方式是:
spe = set("+=/_(*&^%$#@!-.?)")
print(''.join(['*' if c in spe else c for c in a]))
当你到达if语句,如果满足条件就进入分支执行。如果您只想打印语句,您可以 运行:
a = input()
spe = "+=/_(*&^%$#@!-.?)"
for i in a:
if i in spe:
b = "*"
print(b)
else:
b = i
print(b,end="")
但您也可以将其保存为字符串
a = input()
new_string = ""
spe = "+=/_(*&^%$#@!-.?)"
for i in a:
if i in spe:
new_string += "*"
else:
new_string += i
print(new_string)
一个简单的方法:
import string
all_chars = string.ascii_letters
a = 'L!ve l@ugh l%ve'
for item in a:
if item ==' ':
pass
elif item not in all_chars:
item='*'
print(item, end="")
你的脚本有两个问题:
- 如果发现特殊字符,则将其替换为 b 而不是 I
- 只有在找不到特殊字符时才打印。
尝试:
a = "L!ve l@ugh l%ve"
spe = "+=/_(*&^%$#@!-.?)"
for i in a:
if i in spe:
b = i.replace(i,"*")
else:
b = i
print(b,end="")
或者,当我们替换原字符串中的字符时,我们可以在最后打印:
a = "L!ve l@ugh l%ve"
spe = "+=/_(*&^%$#@!-.?)"
for i in a:
if i in spe:
b = a.replace(i,"*")
else:
b = a
print(b)
您还应该考虑使用正则表达式:
import re
a = "L!ve l@ugh l%ve"
print(re.sub("[+\=\/_\(*&^%$#@\!-\.\?\)]","*",a))
它具有完全相同的输出。
作为另一种选择,您可以尝试使用正则表达式。根据您想要定义字符集的方式,这里有两种可能的方法:
import re
print(re.sub('[^a-zA-Z\d\s]', '*', "L!ve l@ugh l%ve"))
print(re.sub("[$&+,:;=?@#|'<>.^*()%!-]", '*', "L!ve l@ugh l%ve"))
# L*ve l*ugh l*ve
我正在尝试编写一个代码来查找特殊字符并将它们替换为 *
。
例如:
L!ve l@ugh l%ve
这应该更改为
L*ve l*ugh l*ve
这是我到目前为止尝试过的
a = input()
spe = "+=/_(*&^%$#@!-.?)"
for i in a:
if i in spe:
b = a.replace(i,"*")
else:
b = i
print(b,end="")
这个returns像这样
Lve lugh lve
为什么我会变成这样?
以及如何解决这个问题?
您正在尝试修改整个字符串,而您应该只修改字符。
修改您的代码,这将是:
a = '(L!ve l@ugh l%ve)'
spe = set("+=/_(*&^%$#@!-.?)") # using a set for efficiency
for char in a:
if char in spe:
print('*', end='')
else:
print(char, end='')
输出:*L*ve l*ugh l*ve*
更像 pythonic 的方式是:
spe = set("+=/_(*&^%$#@!-.?)")
print(''.join(['*' if c in spe else c for c in a]))
当你到达if语句,如果满足条件就进入分支执行。如果您只想打印语句,您可以 运行:
a = input()
spe = "+=/_(*&^%$#@!-.?)"
for i in a:
if i in spe:
b = "*"
print(b)
else:
b = i
print(b,end="")
但您也可以将其保存为字符串
a = input()
new_string = ""
spe = "+=/_(*&^%$#@!-.?)"
for i in a:
if i in spe:
new_string += "*"
else:
new_string += i
print(new_string)
一个简单的方法:
import string
all_chars = string.ascii_letters
a = 'L!ve l@ugh l%ve'
for item in a:
if item ==' ':
pass
elif item not in all_chars:
item='*'
print(item, end="")
你的脚本有两个问题:
- 如果发现特殊字符,则将其替换为 b 而不是 I
- 只有在找不到特殊字符时才打印。
尝试:
a = "L!ve l@ugh l%ve"
spe = "+=/_(*&^%$#@!-.?)"
for i in a:
if i in spe:
b = i.replace(i,"*")
else:
b = i
print(b,end="")
或者,当我们替换原字符串中的字符时,我们可以在最后打印:
a = "L!ve l@ugh l%ve"
spe = "+=/_(*&^%$#@!-.?)"
for i in a:
if i in spe:
b = a.replace(i,"*")
else:
b = a
print(b)
您还应该考虑使用正则表达式:
import re
a = "L!ve l@ugh l%ve"
print(re.sub("[+\=\/_\(*&^%$#@\!-\.\?\)]","*",a))
它具有完全相同的输出。
作为另一种选择,您可以尝试使用正则表达式。根据您想要定义字符集的方式,这里有两种可能的方法:
import re
print(re.sub('[^a-zA-Z\d\s]', '*', "L!ve l@ugh l%ve"))
print(re.sub("[$&+,:;=?@#|'<>.^*()%!-]", '*', "L!ve l@ugh l%ve"))
# L*ve l*ugh l*ve