Python (Scripting) - Remove new lines using strip, lstrip and rstrip

by
Jeremy Canfield |
Updated: March 28 2023
| Python (Scripting) articles
strip can be used to remove new lines. Take for example this script.
#!/usr/bin/python
foo = "Hello World\n"
print(foo)
print("i am here")
The following should be printed, showing a new line after Hello World.
Hello World
i am here
Here is how strip can be used.
#!/usr/bin/python
foo = "Hello World\n"
foo = foo.strip()
print(foo)
print("i am here")
Which should now print the following, showing the new lines after Hello World has been removed.
Hello World
i am here
Or lstrip, to only remove new lines to the left.
#!/usr/bin/python
foo = "Hello World\n"
foo = foo.lstrip()
print(foo)
print("i am here")
Or rstrip, to only remove new lines to the right.
#!/usr/bin/python
foo = "Hello World\n"
foo = foo.rstrip()
print(foo)
print("i am here")
Did you find this article helpful?
If so, consider buying me a coffee over at