+ 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?

15th Nov 2020, 7:17 PM
Yasamin Kholafaei
Yasamin Kholafaei - avatar
16 Answers
+ 31
1) no need for loop (str.replace(old, new) already replaces all ) 2) txt_1 or txt1 not txt 1
15th Nov 2020, 7:50 PM
Sousou
Sousou - avatar
+ 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.
15th Nov 2020, 7:34 PM
Jayakrishna 🇮🇳
+ 6
Use this: txt = input() txt1 = txt.replace('#', ' ') print(txt1) Don't use Ridiculous spaces and loops.
15th Nov 2020, 7:42 PM
Mohsen Abbaspour
Mohsen Abbaspour - avatar
+ 4
print(input().replace('#',' '))
15th Jun 2021, 10:59 AM
Akash Sharma
Akash Sharma - avatar
+ 2
Dont use for loop txt = input() txt1 = txt.replace('#', ' ') print(txt1)
7th Sep 2021, 8:10 PM
Ismail
+ 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)
26th Jul 2021, 7:13 AM
Rey John Gonzales
Rey John Gonzales - avatar
0
😶
16th Nov 2020, 2:06 PM
Hossen Mahmod
Hossen Mahmod - avatar
0
Yes its true
4th Jul 2021, 5:59 PM
Hamza Rahali
Hamza Rahali - avatar
8th Jan 2022, 6:13 AM
Ayush Suri
0
Heyo! You can easily go with txt.replace(#,’ ’) as it will apply to the whole file. :)
23rd Apr 2022, 11:41 PM
Bartosz Kubiak
Bartosz Kubiak - avatar
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))
9th May 2022, 7:31 AM
SAMOU DIASSANA
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('#', ' '))
14th Jul 2022, 5:49 AM
Konda Sandeep
Konda Sandeep - avatar
0
The name of a variable doesn't take space on it. You don't need to use a loop.
28th Nov 2022, 10:52 AM
Jaime
Jaime - avatar
0
I solved this way msg = input() output_message = msg.replace("#", " ") print(output_message)
24th Jul 2023, 2:00 AM
Mushfiqur Rahman
Mushfiqur Rahman - avatar
- 1
Hi
17th Nov 2020, 2:17 PM
the gaming star
the gaming star - avatar
- 2
Good
16th Jun 2021, 6:17 PM
محمد سالم عبد العباس محمود
محمد سالم عبد العباس محمود - avatar