Introduction
Sometimes, we occur in a situation where we want to return 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.
What is a carriage return (\r) in Python?
It helps us move the cursor at the beginning of the line without moving it to the new line.
Ways to use carriage return
We will show 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 strings.
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, 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 use ‘\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 applied tab space at the beginning of the string, giving 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 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 use \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 relevant for the programs that process “plain text,” and there are not 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. I also understood all the ways through which we can use ‘\r’ in- different ways in detail with the help of an example. All the examples are explained in detail.
However, please let me know in the comment section below if you have any questions. I will try to help you as soon as possible.
Kudos for that, that was Super!
Keep them coming, Bless you x
string = ‘My web\nsite is Latracal \rSolution’
print(string)
The output:
My web
SolutionLatracal
I don’t understand this.
The o/p i run on the compiler came out differentlyi.e,
My web
site is Latracal
Solution
> o/p i thought is
My web
Solutionctracal
\r
does not stand for a new line.\r\n
does.