Python carriage return vs newline

What’s the difference between \n (newline) and \r (carriage return)?

In particular, are there any practical differences between \n and \r? Are there places where one should be used instead of the other?


I would like to make a short experiment with the respective escape sequences of \n for newline and \r for carriage return to illustrate where the distinct difference between them is.

I know, that this question was asked as language-independent. Nonetheless, We need a language at least in order to fulfill the experiment. In my case, I`ve chosen C++, but the experiment shall generally be applicable in any programming language.

The program simply just iterates to print a sentence into the console, done by a for-loop iteration.


Newline program:

#include <iostream>

int main(void)
{
    for(int i = 0; i < 7; i++)
    {
       std::cout << i + 1  <<".Walkthrough of the for-loop \n";   // Notice `\n` at the end.
    }
    return 0;
}

Output:

1.Walkthrough of the for-loop
2.Walkthrough of the for-loop
3.Walkthrough of the for-loop
4.Walkthrough of the for-loop
5.Walkthrough of the for-loop
6.Walkthrough of the for-loop
7.Walkthrough of the for-loop

Notice, that this result will not be provided on any system, you are executing this C++ code. But it shall work for the most modern systems. Read below for more details.


Now, the same program, but with the difference, that \n is replaced by \r at the end of the print sequence.

Carriage return program:

#include <iostream>

int main(void)
{
    for(int i = 0; i < 7; i++)
    {
       std::cout << i + 1  <<".Walkthrough of the for-loop \r";   // Notice `\r` at the end.
    }
    return 0;
}

Output:

7.Walkthrough of the for-loop 

Noticed where the difference is? The difference is simply as that, when you using the Carriage return escape sequence \r at the end of each print sequence, the next iteration of this sequence do not getting into the following text line - At the end of each print sequence, the cursor did not jumped to the *beginning of the next line.

Instead, the cursor jumped back to the beginning of the line, on which he has been at the end of, before using the \r character. - The result is that each following iteration of the print sequence is replacing the previous one.

*Note: A \n do not necessarily jump to the beginning of following text line. On some, in general more elder, operation systems the result of the \n newline character can be, that it jumps to anywhere in the following line, not just to the beginning. That is why, they rquire to use \r \n to get at the start of the next text line.


This experiment showed us the difference between newline and carriage return in the context of the output of the iteration of a print sequence.

When discussing about the input in a program, some terminals/consoles may convert a carriage return into a newline implicitly for better portability, compatibility and integrity.

But if you have the choice to choose one for another or want or need to explicitly use only a specific one, you should always operate with the one, which fits to its purpose and strictly distinguish between.

  • Introduction
  • What is a carriage return (\r) in Python?
  • Ways to use carriage return
    • 1. Using only carriage return in Python
    • 2. Using carriage return in Python with a newline character
    • 3. Using Carriage return in python with tab space
    • 4. Using carriage return in python, tab space and newline character
  • How \r and \n is handled on Linux and windows
  • Must Read
  • Conclusion

Introduction

Sometimes, we occur in a situation where we want to go back to the starting point of the same line. In this article will help you understand the concept of carriage return in python or \r in python.

It helps us move the cursor at the beginning of the line without moving the cursor to the new line.

Ways to use carriage return

We will showing all the types by which we can use the ‘\r’ in python.

1. Using only carriage return in Python

In this example, we will be using only the carriage return in the program in between the string.

string = 'My website is Latracal \rSolution'

print(string)

Output:

Solutionte is Latracal

Explanation:

  • Firstly, we have taken an input string as a string.
  • we have applied \r in between the string.
  • \r has shifted the cursor to the start, and ‘solution’ contains 8 letters. From the beginning, 8 letters will be erased, and in place of that solution gets printed.
  • You can see the output for better understanding.

2. Using carriage return in Python with a newline character

In this example, we will be using ‘\r’ with the new line character(\n) in the string program.

string = 'My website is Latracal \r\nSolution'
print(string)

string = 'My website is Latracal \n\rSolution'
print(string)
string = 'My web\nsite is Latracal \rSolution'

print(string)

Output:

My website is Latracal 
Solution
My website is Latracal 
Solution
My web
SolutionLatracal

Explanation:

  • Firstly, we have taken an input string as a string.
  • Then, we have applied \n and \r in multiple places of the string.
  • \n is for newline.
  • In the first two strings, we have put \n before and after \r. so the output gets printed in a newline.
  • In the last string, \n is first after the ‘site is, ‘which contains 8 letters as solution, so they get replaced.
  • Hence, you can see the output.

3. Using Carriage return in python with tab space

In this example, we will be using the carriage or \r with the combination of tab space or \t in the program between the string.

str = ('\tLatracal \rsolution')
print(str)

Output:

solutionLatracal 

Explanation:

  • Firstly, we have taken an input as str.
  • Then, we have applied tab space at the beginning of the string, which will give 8 spaces at the beginning.
  • Then, we have applied \r. After \r, there are 8 letters of solution.
  • In the output, the letter of solution will fill the tab spaces as they are equal.
  • You can see the output for better understanding.

4. Using carriage return in python, tab space and newline character

In this example, we will be mixing all the characters such as carriage return(\r), tab space(\t), and newline character(\n) in the given string and see the output so that we can understand the use to \r more clearly.

str = ('\tlatracal\rsolution\n\tis a\rwebsite\n')
print(str)

Output:

solutionlatracal
website is a

Explanation:

  • Firstly, we have taken an input string as str.
  • Then, we have applied all the characters like \t for tab space, \n for the new-line, and \r for carriage return.
  • Hence you can see the output.

How \r and \n is handled on Linux and windows

As we all know, we are using \r for carriage return and \n for newline in windows. But, for different operating systems, there are different conventions. The difference is simple, i.e., OS designers had to choose how we should represent a new line in text in computer files. For some reason, in Unix/Linux world, a single LF(Line feed) was chosen as the new line marker. MS-DOS chose CR+LF, and windows inherited \n as a new line. Thus, we got to know that different platforms have different conventions.

In practice, this is becoming a shorter of a problem. The newline marker is basically relevant for the programs that process “plain text,” and there are not that many. This mostly only affects program source code, configuration files, and some simple text files with documentation. As in today’s world, most programs handling these kinds of files (editors, compilers, etc.) can handle both newline conventions, so it does not matter which one you choose.

Must Read

Conclusion

In this tutorial, we have learned about the concept of carriage return (‘\r’) with its definition. Also understood all the ways through which we can use ‘\r’ by different- different ways in detail with the help of an example. All the examples are explained in detail.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Is \r and \n the same?

\r stands for carriage return Both \n and \r are different characters and were used as end of line terminators in different operating systems. \r – was previously used as the end-of-line terminator in Mac text files, now \n is also used. \n\r -are used to terminate lines in the DOS and Windows text files.

Is \n line feed or carriage return?

A line feed means moving one line forward. The code is \n . A carriage return means moving the cursor to the beginning of the line. The code is \r .

What does carriage return do in Python?

\r takes the cursor to the beginning of the line. It is the same effect as in a physical typewriter when you move your carriage to the beginning and overwrite whatever is there.

What is \r in Python example?

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.

Tải thêm tài liệu liên quan đến bài viết Python carriage return vs newline