Back
There are several methods to do this I am discussing a few with you:
Use rstrip
>>> 'test string\n'.rstrip()'test string'
>>> 'test string\n'.rstrip()
'test string'
To use this for newlines:
>>> 'test string \n \r\n\n\r \n\n'.rstrip('\n')'test string \n \r\n\n\r '
>>> 'test string \n \r\n\n\r \n\n'.rstrip('\n')
'test string \n \r\n\n\r '
You can also use lstrip() and strip()
>>> s = " \n\r\n \n abc def \n\r\n \n "
>>> s.strip()'qwe rty'>>> s.lstrip()'qwe rty \n\r\n \n '>>> s.rstrip()' \n\r\n \n qwe rty'
If you are looking for upskilling yourself in python you can join our Python Certification and learn from the industrial expert.
31k questions
32.8k answers
501 comments
693 users