+ 8
Python functions
This is the task : Your friend sent you a message, however his keyboard is broken and types a # instead of a space. Replace all of the # characters with spaces and output the result And its the code : txt = input() for x in txt: txt 1 = x.replace(#, ' ') print(txt 1) Whats the problem with this code?
16 Antworten
+ 31
1) no need for loop (str.replace(old, new) already replaces all )
2) txt_1 or txt1 not txt 1
+ 11
There no need of loop..
txt.replace('#',' ')
Replaces # with space in total original string..
Check it by print(txt)
One liner :
print(input().replace('#', ' '))
Edit :
Yasamin Kholafaei
In your code in variable txt 1, space is not alloved to use in variable. You should remove. => txt1
And you forget to add quotes for charecter # in replace function like '#'. So add quotes for #, and remove space in txt 1.
+ 6
Use this:
txt = input()
txt1 = txt.replace('#', ' ')
print(txt1)
Don't use Ridiculous spaces and loops.
+ 4
print(input().replace('#',' '))
+ 2
Dont use for loop
txt = input()
txt1 = txt.replace('#', ' ')
print(txt1)
+ 1
Just play using for loop:
sentence="Hi#Everyone#how#are#you#today?"
print(sentence)
res=""
for i in sentence:
if i=="#":
i=" "
res+=i
print(res)
0
😶
0
Yes its true
0
Heyo! You can easily go with txt.replace(#,’ ’) as it will apply to the whole file. :)
0
Quel est le résultat de ce code ? triple = lambda x : x * 3 ajouter = lambda x, y : x + y imprimer(ajouter(triple(3), 4))
0
Python functions
txt = input()
for x in txt:
txt 1 = x.replace(#, ' ')
print(txt 1)
Answer :
1. txt_1 or txt1 ( txt 1 ,naming should be like this ) because spacing is not allowed
2. replace we can directly use on string instead of each character ( this is not the issue )
3. print will end with newline
You have to use print(txt_1, end='')
Overall answer will be
txt = input()
for x in txt:
txt_1 = x.replace(#, ' ')
print(txt_1, end='')
Answer simple way
print(input().replace('#', ' '))
0
The name of a variable doesn't take space on it. You don't need to use a loop.
0
I solved this way
msg = input()
output_message = msg.replace("#", " ")
print(output_message)
- 1
Hi
- 2
Good