How to Add CRLF to a String in Python

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.
Image Credit: matejmo/iStock/GettyImages

In Python, CRLF refers to a carriage return and line feed. This pair of characters is used to end lines of text in many computer files, modeled after the actions a typewriter user takes at the end of a line of typing. In Python, you can manually add them to a string using string concatenation operators, or you can specify they should be added to a string you print to the screen or a file.

Advertisement

Python and CRLF

Video of the Day

Different operating systems have different ways to indicate when a text file includes a line break. It's usually done with some mix of the carriage return and line feed characters, which are defined by particular numeric codes.

Advertisement

Video of the Day

Traditionally, Microsoft operating systems use a carriage return followed by a line feed, and Unix systems including Linux and recent versions of Apple macOS only use a single line feed. Older Mac systems used only a single carriage return.

Many text processing programs can detect which line endings are used and open any file so that it displays normally, but text can occasionally be mangled if a program isn't set up to make this guess or does so incorrectly. Python is a programming language that runs on a wide variety of systems, so it needs to be able to handle a variety of types of line endings.

Advertisement

Adding Line Endings to Strings

Manually add a line ending of your choice to a string in Python using the plus sign concatenation operator, which joins together multiple strings.

For example, "abc" + "def" yields the string "abcdef" as a result. In Python, a carriage return is represented by the string \r and a newline character is represented by the string \n. The backslash is an escape character that tells Python that the following character has a special meaning. To type an actual backslash, put a second backslash before it to have Python escape it as well.

Advertisement

Advertisement

To add a carriage return and newline to a string, add the string "\r\n" to it using the plus sign.

Printing With CRLF

By default, the print operator in Python 2 and the print function in Python 3 add only a newline character, not a carriage return, to the end of each line of text printed to a string or a file. Both Python 2 and Python 3 are widely in use, but they handle printing slightly differently.

Advertisement

On Python 2, you add the correct ending to each line yourself and append a comma to the end of the print statement after the input to suppress Python's own line ending. For example, you could enter 'print "This is a test" + "\r\n"' to print the string "This is a test" with a carriage return and newline.

Advertisement

In Python 3, "print" is a function rather than a special operator. It takes an argument called "end" that specifies the line ending to use. 'Print ("This is a test", end = "\r\n")' prints the sentence with a carriage return and newline character. By default, the end argument is simply a newline character.

Advertisement

Advertisement

references

Report an Issue

screenshot of the current page

Screenshot loading...