Home python Python, Replace

Python, Replace

Author

Date

Category

Using Read I read the file in which the “Hello” string. I need it to be replaced with the help of the Replace with “Hello” on “Hello, (word from the Input), Ola.” I am not surprised that nothing happened. It seems like a lot of commas. In short, Hello does not change what I wrote. MB help?)

nf2 = input ("Word:")
new_data = old_data.replace ('Hello', 'Hello', NF2, 'Ola')

Answer 1

REPLACE does not work as you wrote. It can change 1 value to another (not much immediately). Therefore, you must first form a string to replace the word Hello, and then replace.

f = open ('file.txt', 'r')
Data = F.Read ()
F.Close ()
INPUT = INPUT ()
String = "Hello,"
String + = Input
String + = "OLA" # Now String - Finished Row
new_data = data.replace ("Hello", String) # replacement
File Open ('file.txt', 'W') # and file entry
Data = F.Write (NEW_DATA)
F.Close ()

Answer 2

just fold the strings with the fact that it was introduced:

nf2 = input ("Word:")
Text = 'Hello,' + NF2 + ', Ola'
new_data = old_data.replace ('Hello', Text)

or use string formatting:

text = f'hello, {nf2}, ola '

still (this was before the appearance of the F-strings):

text = 'hello, {}, ola'.format (NF2)

More (this is already used):

text = 'hello,% s, ola'% nf2

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions