Here is my current code. I would like it to be in one line I don't know how to though so I came here.
i = input("enter here")
for x in range(0,len(i)):
print(i.replace(i[x],"123"))
I also tried:
print('',(i:= input("enter here")).join(i.replace(i[x],'123') for x in len(i)))
However that doesn't work.
CodePudding user response:
Since your question wasn't "completely clear". Such as what the output is supposed to be, and if you wanted to compress your code into 1 line or not...
But try this:
#1st code~ returns output like:
['123ello', 'h123llo', 'he123123o', 'he123123o', 'hell123']
#2nd code ~ returns output like:
123123123123123
(@Tim Roberts's comment)
1 line versions
#1
i = input(">");print([i.replace(i[d],"123") for d in range(len(i))])
#2
x = input(">");print('123' * len(i))
Multiline versions
#1
i = input(">")
x = []
for d in range(len(i)):
x.append(i.replace(i[d],"123"))
print(x)
#2
x = input(">")
print('123' * len(i))
CodePudding user response:
The following code works with regex. Does it give the desired result?
import re
i = 'i2t'
print(re.sub('[^\W\d_]', '123', i))
The output from above will be: 1232123