Remove newlines from a string
Write a Python program to remove newline characters from a string.
"objectoriented\nprogramming\n"
'objectoriented\nprogramming' 'objectorientedprogramming'
Approach
strip()for the ends,replace("\n", "")for everywhere.
Solution
text = "objectoriented\nprogramming\n"print(repr(text.rstrip("\n")))print(repr(text.replace("\n", "")))'objectoriented\nprogramming' 'objectorientedprogramming'
Line by line
rstrip("\n") removes only trailing newlines.
replace removes them everywhere.
Learn the concept
New to this topic? Read the lesson, then come back.