0

Windows:

Uses CR (\r) in combination with LF (\n) for line endings, facilitating compatibility with legacy systems.

Unix-like systems (including Linux and macOS):

Employs LF (\n) for line endings, offering simplicity and compatibility with text processing utilities.

Older Macintosh systems:

Uses CR (\r) for line endings in some file formats, retaining compatibility with legacy systems.

Question:

When I write the following in Notepad++ on Windows with all symbols displayed, I get this:

Notepad++

But when I write a simple code in Python also on Windows, I get this in the terminal:

print('Hello \nWorld')

enter image description here

It seems that Windows uses the CR LF as a convention for every new line, but why does Python compile without the CR if I only write \n and not \r\n as an equivalent to CR LF?

asked Feb 16, 2024 at 14:52
11
  • 4
    I am 95% sure python (or the platform libraries it calls into) transparently turns that \n into \r\n when on Windows Commented Feb 16, 2024 at 15:01
  • @whatsisname I am trying to find that in the Python repo somehow... Commented Feb 16, 2024 at 15:05
  • 1
    @DevelBase2 Looking in "the Python repo" (by which I assume you mean the CPython source) does not say what Python does. Commented Feb 16, 2024 at 15:07
  • 3
    See docs.python.org/3/library/functions.html#open and os.linesep Commented Feb 16, 2024 at 15:10
  • 1
    @DevelBase2 correct - most languages have this in their file handling somewhere, a means to automatically output the correct line endings for text files on the current platform. This may also apply to the standard output stream used by print(). Commented Feb 16, 2024 at 15:25

1 Answer 1

9

Windows uses CR (\r) in combination with LF (\n) for line endings, facilitating compatibility with legacy systems.

Well, no. A windows system stores text files on the hard drive with whatever style of line endings you gave them.

If you follow the windows convention most windows programs that read these files will interpret the line endings correctly.

Some, like python interpreters, notepad++, and eclipse will interpret the line endings correctly whichever convention the file follows.

Others won’t. It’s not the file system that makes the difference here. Or the operating system. It’s whatever’s reading and saving the file. It’s only a windows thing because one style is most popular in windows.

answered Feb 16, 2024 at 17:10
1
  • 1
    Plus for NP++ and most text editors you can just change which kinds of newlines are saved Commented Feb 19, 2024 at 12:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.