Write a Python program to remove a newline in Python


This program is written in the Python programming language and it removes the newline characters ("\n") from a string. It does this in two steps:

  • The first step is to define the input string "str" which contains multiple newline characters.
  • The program then prints the original string "str" to the console using the "print" function. The output will show each word of the string on a newline.
  • The program then uses the "splitlines" method to split the string "str" by its newline characters, resulting in a list of individual words. The "join" function is then used to concatenate the list of words into a single string without any newline characters. This final string is then printed to the console using the "print" function.

Source Code

"""
str1="Tutor Joes\n"
#Before Remove NewLine(\n)
print(str1)
#After Remove NewLine(\n)
print(str1.rstrip())
 
str1="\nTutor Joes\n"
#Before Remove NewLine(\n)
print(str1)
#After Remove NewLine(\n)
print(str1.strip())
"""
str = "\nTutor \nJoes \nComputer \nEducation\n"
#Before Remove NewLine(\n)
print(str)
#After Remove NewLine(\n)
print(''.join(str.splitlines()))

Output

Tutor
Joes
Computer
Education

Tutor Joes Computer Education


Example Programs