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.
What is a carriage return (\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.
The post Working With Carriage Return (\r) in Python appeared first on Python Pool.
from Planet Python
via read more
No comments:
Post a Comment