Skip to content Skip to sidebar Skip to footer

Python Newline Constant Like C++ Std::endl

I am formatting a string (in magic method __str__) and I don't want to use a raw '\n' or '\r\n'. Does python have a cross-platform newline identifier like std::endl in C++? I tried

Solution 1:

How about using os.linesep? It contains the appropriate line separator for your OS:

>>>import os>>>os.linesep
'\n'
>>>print"line one" + os.linesep + "line two"
line one
line two

Solution 2:

No. Use \n. And in C++, std::endl is nothing other than "\n" << std::flush, and it is not more cross-platform, whatever that would mean. It's only slower, as it forces flushing.

Solution 3:

There is nothing wrong with using \n - why don't you want to use it? The cross-platform problem is only relevant when writing to files, and the file-object takes care of that for you.

Post a Comment for "Python Newline Constant Like C++ Std::endl"