+ 3
how to make all of this replacement with one code
name=x.replace("1","") name1=name.replace("2","") name2=name1.replace("3","") name3=name2.replace("4","") name4=name3.replace("5","") name5=name4.replace("6","") name6=name5.replace("7","") name7=name6.replace("8","") name8=name7.replace("9","") name9=name8.replace("`","") name10=name9.replace("=","") name11=name10.replace("+","") name12=name11.replace("
quot;,"") name13=name12.replace("%","") name14=name13.replace("&","") name15=name14.replace("^","") name16=name15.replace("*","") name17=name16.replace("#","") name18=name17.replace("@","") name19=name18.replace("!","") name20=name19.replace("/","") name21=name20.replace("_","") name22=name21.replace("-","")9 Answers
+ 4
I would use the filter function. Here is an example that consolidates all of your replacements and does the same thing in one line:
name = "Mr. Jack $Benny$ the 3rd"
name1 = "".join(filter(lambda c: (c not in "123456789`=+$%&^*#@!/_-"), name))
print(name, name1, sep='\n')
Output:
Mr. Jack $Benny$ the 3rd
Mr. Jack Benny the rd
+ 6
a) put all to be replaced characters in a list/tuple or a string. Iterate over the list/ string and replace inside of the loop
b) if you want to get rid of all non-letter characters, you can try str.isalpha(): copy the input to a 2nd variable, iterate of input and replace if isalpha == False in the copied string
c) use regular expressions like re.sub(r"[^a-zA-Z]", "", string)
+ 5
Mostafa Ahmad what is this reverse function supposed to do there? Remove it. The loop needs to be something like
for y in symbol:
x = x replace(y, "")
You need to apply the replacements to the same string.
After the loop x will be the string without the symbols
+ 3
nums = list(map(str, range(10)))
syms = "`=+$%&^*#@!/_-"
x = "Hello,2022 it's @me" #input()
for n in nums:
x = x.replace(n, "")
for s in syms:
x = x.replace(s, "")
print(x)
https://code.sololearn.com/cKJxy2pH24lv
+ 2
import re
txt = "Hi, my phone number is 01234456677. I am 25 years old. My password is dg%&%?Afgh"
def replaceString(txt):
txt = re.sub('[^a-z A-Z]', '', txt)
print(txt)
replaceString(txt)
+ 1
x=input()
def function(y):
return y[::-1]
symbol=["+","Ć","Ć·","=","/","_","ā¬","Ā£","Ā„","ā©","!","@","#","%","^","&","*","(",")","-"]
for y in symbol :
z=x.replace(y,"")
print(function(z))
+ 1
when I run the programme no thing replaced š
+ 1
Lisa thank you š
+ 1
SoloProg thank you š