How to remove all newlines from a string in Python

Newlines are encoded as the string “\n”. Removing all newlines from a string results in a new string excluding any occurrence of “\n”. For example, removing newlines from “abc\ndef” results in “abcdef”.

Solution for How to remove all newlines from a string in Python : You can use a str.replace() to remove all newlines from a string Call str.replace(old, new) with old as “\n” and new as “” to remove all newlines from str.


how-to-remove-all-newlines-from-a-string-in-python