Home About project

Remove line breaks with Python

If you need to remove line breaks from text with Python you can use next string method:

  • replace(old, new [, count])

Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

Line breaks in text are generally represented as:

  • \r\n - on a windows computer
  • \r - on an Apple computer
  • \n - on Linux

To remove line breaks using Python see example:

    //Removes all 3 types of line breaks
    string = string.replace("\r","")
    string = string.replace("\n","")