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')

Run this code


Python is included in CodeSpeedy

Noe see what happens if I use a carriage return here

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

Run this code


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.

20 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.

  3. Maneesh2 says:

    Hey, I also think that your code is not working.

    Input: print(“My name is Maneesh\r123456”)
    Output: My name is Maneesh123456

    Maybe your information about carriage return is wrong? Or perhaps there is something wrong with the code? I mean it’s a simple print function, what could ever go wrong unless the ‘\r’ function works in a completely different way.

    • Saruque Ahamed Mollick says:

      To best of my knowledge after searching the internet, I found that some of the GUI based IDE does not work properly with backspace and carriage return character. If you use console it should work fine.
      If you are not using Python3 then it is suggested to use from __future__ import print_function
      There is nothing wrong with your code as this is a simple code.

    • Pradhyumna Narain says:

      I think it’s because you need to have a space after the \r.

    • Om says:

      Maneesh2 Your code is wrong … what ever Sharuque explained is totally correct

  4. Hub. Pol. says:

    Maneesh, if you’re using an IDE like Sublime or something similar, it will not work. It will work if you run your script in your terminal.

  5. amit jain says:

    Dear Saruque Ahamed Mollick ,
    it doesn’t work as per your description.

  6. Bring_Paragon_Back says:

    try the console found at and copy past the following:

    print(‘Hello World!\r123’)

  7. Anshul says:

    It definitely works. The reason it’s not working is that they are trying it in IDEz. If you write your code in notepad. save the file then run it on cmd , it will work according to what has been taught on this page. Thanks codespeedy. !

  8. Ruben says:

    It depends on your operating System! Windows line-ending use both the carriage return (\r) and a newline (\n). Mac / Linux uses a newline. Pre-OS X (Mac) use carriage return only.

    If you are on Windows, use “\r\n”:
    print (“my name is \r\nRuben the great!”)

    If you are on Mac or Linux, use “\n”
    print(“my name is \nRuben the Great!”)

  9. Auds says:

    Ruben: \r\n is what worked for me. On windows. Cool! Thanks!

  10. Arod says:

    Worked for me on windows powershell! Thanks Codespeedy

  11. Drew says:

    This article was a life saver for me. I am scraping some word doc’s with win32com and was fighting some really strange behavior because it was returning \r in the text. I’m surprised I’ve never ran into this problem before but your article was just what I needed to get things sorted and understand what was actually going on. Thank you for sharing your knowledge.

  12. Dev Patidar says:

    how can I use \r ,If I take user input

  13. rishabh kumar says:

    this use of \r you have discussed is for which version of python because my python version is giving something completely different than this , mine is latest version.

  14. Arindham says:

    It was such an easy topic, I had halfway understood it on my own, but this article helped me know how to use it correctly. For the people for whom this does not work it is definitely due to the IDE or python version. This was not working for me in pycharm or sublime but worked perfectly on spyder and jupyter.

  15. Ashish Udupa says:

    I gave input print(“hello world \r world”)
    It’s showing : hello world
    What’s the error?

Leave a Reply

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