How to Add CRLF to a String in Python

Carriage Return (CR) is a control character that causes the text cursor to move to the beginning of the line. Line Feed (LF) is another control character that causes the cursor to move to the following line. Different operating systems follow different conventions that use one or both of those characters to signal the end of a text line. For example, on the Windows operating systems, a line of text ends with a CR followed by a LF. You can terminate strings with a CR/LF combination to make your Python code follow this convention.

...
Add the appropriate end-of-line control characters to your Python strings.

Step

Compute the value of the string you want to output and assign it to a Python variable, as in the following sample code:

Step

myString = "Average programmer salary"

Step

Add the CR control character to the end of the string by using Python's "+" string concatenation operator:

Step

myCRString = myString+'\r'

Step

Add the LF control character to the end of the string by using Python's "+" string concatenation operator:

Step

myCRLFString = myCRString+'\n'

Step

In the example, variable "myCRLFString" contains the initial string value with CR and LF added for correct output.