what does the '\r' mean in strings? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

what does the '\r' mean in strings?

I understood the splitlines method using '\n', but the '\ r' in this example, just confuse me..... str = 'ab c\n\nde fg\rkl\r\n' print(str.splitlines()) str = 'ab c\n\nde fg\rkl\r\n' print(str.splitlines(True)) >>> ['ab c', '', 'de fg', 'kl'] ['ab c\n', '\n', 'de fg\r', 'kl\r\n']

27th Dec 2018, 7:52 PM
Razf
Razf - avatar
2 Answers
+ 4
\r is a carriage return whereas \n is a new line (imagine an old typewriter machine changes line in two steps; roll the paper and send the carriage back to the left) In some OS (such as linux), \n alone is the standard and \r is ignored in most cases. Windows prefers the full cr+lf (\r\n).
27th Dec 2018, 8:17 PM
Claude Desjardins
Claude Desjardins - avatar
+ 1
I'm not expert in Python but in documents it is described as "carriage return". Sometimes used in pair with "\n" literal. You can find more info here: https://docs.python.org/release/2.2.3/ref/strings.html and here: https://stackoverflow.com/questions/14606799/what-does-r-do-in-the-following-script.
27th Dec 2018, 8:15 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar