How does carriage return “\r” work in python

In this Python tutorial, we will learn how does carriage return “\r” work in Python. A carriage return is a special type of escaping character. Many of the Python learners have noticed that \r\n is used in Python. Most of them know the work function of the \n new line in Python. But few of them know about the working function of a carriage return in Python.

So today we will learn what does carriage return “\r” do in Python.

What is Carriage Return in Python or what is \r in Python

A carriage return is nothing but a simple escape character. \n is also an escape character which creates a new line.

Carriage return or \r is a very unique feature of Python. \r will just work as you have shifted your cursor to the beginning of the string or line.

Whenever you will use this special escape character \r, the rest of the content after the \r will come at the front of your line and will keep replacing your characters one by one until it takes all the contents left after the \r in that string.

How does Carriage Return \r work in Python

Let’s understand it with some examples.

print('Python is included in CodeSpeedy')


Python is included in CodeSpeedy

Noe see what happens if I use a carriage return here

print('Python is included in CodeSpeedy\r123456')


123456 is included in CodeSpeedy

You can see here we have used \r – carriage return after “Python is included in CodeSpeedy

So whatever content is there after the \r will come at the beginning of our whole string.

So Python will be replaced by 123456

You may also learn, How to escape from special characters in python

As 123456 are having 6 characters so the first 6 characters of our string that is Python will be replaced by 123456

If I do the below:

print('Python is included in CodeSpeedy\r123456789')


123456789 included in CodeSpeedy

Now you can see 123456789 are having 9 characters so first 9 characters are being replaced by those 9 numbers.

Even space will be considered as a character.

Here is another example of carriage return,

print('Hey there I am busy in learning Carriage Return in Python\rthis is going to be added')


this is going to be addedarning Carriage Return in Python

This is just like bringing the cursor to the starting position and typing the rest of the characters ( the characters after the \r ) by pressing the ins ( insert key ) key of your keyboard.

2 responses to “How does carriage return “\r” work in python”

  1. maneesh says:

    dude! it’s not working as u said in the description.
    print(‘MY_name_is \r _Maneesh’)
    MY_name_is _Maneesh

  2. Saruque Ahamed Mollick says:

    Dear, Maneesh thanks for submitting your doubt here.
    Your code and the output in Python for carriage return,
    print(‘MY_name_is \r _Maneesh’)

    Output: $ python

    I think you have done something wrong in your code.

Leave a Reply

Your email address will not be published. Required fields are marked *