Python functions | Sololearn: Learn to code for FREE!

+4

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?

11/15/2020 7:17:56 PM

Yasamin Kholafaei

10 Answers

New Answer

+19

1) no need for loop (str.replace(old, new) already replaces all ) 2) txt_1 or txt1 not txt 1

+6

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.

+3

Use this: txt = input() txt1 = txt.replace('#', ' ') print(txt1) Don't use Ridiculous spaces and loops.

+1

print(input().replace('#',' '))

0

0

0

0

Yes its true

0

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

Dont use for loop txt = input() txt1 = txt.replace('#', ' ') print(txt1)