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

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


Please enter 1be3fb in the box below so that we can be sure you are a human.