How Does \R (Carriage Return) Work in Python

how does \r (carriage return) work in Python

Well, it appears that you did move to the left of the line. It just left the rest of the line untouched. Note that return is 6 chars, and I will is also 6.

Use of \r (carriage return) in python regex

.* is greedy in nature so it is matching longest match available in:

r'Some text\r(.*)\r

Hence giving you:

re.findall(r'Some text\r(.*)\r', 'Some text\rText to find !\r other text\r')
['Text to find !\r other text']

However if you change to non-greedy then it gives expected result as in:

re.findall(r'Some text\r(.*?)\r', 'Some text\rText to find !\r other text\r')
['Text to find !']

Reason why re.findall(r'Some text\n(.*)\n', 'Some text\nText to find !\n other text\n') gives just ['Text to find !'] is that DOT matches any character except line break and \n is a line break. If you enable DOTALL then again it will match longest match in:

>>> re.findall(r'Some text\n([\s\S]*)\n', 'Some text\nText to find !\n other text\n')
['Text to find !\n other text']

>>> re.findall(r'(?s)Some text\n(.*)\n', 'Some text\nText to find !\n other text\n')
['Text to find !\n other text']

Which again changes behavior when you use non-greedy quantifier:

re.findall(r'(?s)Some text\n(.*?)\n', 'Some text\nText to find !\n other text\n')
['Text to find !']

Correct method of using a carriage return and newlines in python

Adding to alefir's answer, the closest I could come up with was something like this, which puts everything you want to change onto one line. Not sure if it meets the need for your code.

import time

counter1 = 0
counter2 = 0
counter3 = 0

print("%s\t%s\t%s" % ("Counter 1 name", "Counter 2 name", "Counter 3 name"))
for i in range(5):
counter1 += 1
counter2 += 1
counter3 += 1
print("%.1f\t\t%.1f\t\t%.1f" % (counter1, counter2, counter3), end = "\r")

Alternatively, to implement alefir's approach with multiple lines, you could use something like this to clear the screen:

# import only system from os
from os import system, name

# define our clear function
def clear():

# for windows
if name == 'nt':
_ = system('cls')

# for mac and linux(here, is 'posix')
_ = system('clear')

Combining with your attempt 1, you would have something like this:

for i in range(5):
counter1 += 1
counter2 += 1
counter3 += 1
Counter 1: {counter1}
Counter 2: {counter2}
Counter 3: {counter3}

What does "\r" do in the following script?

The '\r' character is the carriage return, and the carriage return-newline pair is both needed for newline in a network virtual terminal session.

From the old telnet specification (RFC 854) (page 11):

The sequence "CR LF", as defined, will cause the NVT to be
positioned at the left margin of the next print line (as would,
for example, the sequence "LF CR").

However, from the latest specification (RFC5198) (page 13):

  1. ...

  2. In Net-ASCII, CR MUST NOT appear except when immediately followed
    by either NUL or LF, with the latter (CR LF) designating the "new
    line" function. Today and as specified above, CR should
    generally appear only when followed by LF. Because page layout
    is better done in other ways, because NUL has a special
    interpretation in some programming languages, and to avoid other
    types of confusion, CR NUL should preferably be avoided as
    specified above.

  3. LF CR SHOULD NOT appear except as a side-effect of multiple CR LF
    sequences (e.g., CR LF CR LF).

So newline in Telnet should always be '\r\n' but most implementations have either not been updated, or keeps the old '\n\r' for backwards compatibility.

Related Topics

Leave a reply