Given: Assume you have a following text file (sample.txt).
Line1 line2 line3 line4 line5
Expected Output:
Hint:
replace()
\n
' '
Solution:
with open('sample.txt', 'r') as file: data = file.read().replace('\n', ' ') print(data)
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Hint:
replace()function to replace all newlines (\n) with space (' ').Solution:
- First, open the file in a read mode
- Next, read all content from a file using the read() function and assign it to a variable.
- Next, use string
- Display final string
need an explanation for this answer? contact us directly to get an explanation for this answerreplace()function to replace all newlines (\n) with space (' ').