Home Python How To Use Python Carriage Return?

How To Use Python Carriage Return?

0
How To Use Python Carriage Return?

There may be situations where you desire to return to the beginning of the same line. This article will help you understand the concept of Python carriage return or Python\r.

What is a Python carriage return (\r)?

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

How to use carriage return?

It speaks for any type of “\r” that can be used in Python.

  1. Only use Carriage Returns in Python:

In this example, the program between strings only uses carriage returns.

 String = 'My Website Lateral \ Resolution.'
 String

Output:

The solution is the la trace.

Description:

  • First, I got the input string as a string.
  • I applied \ r between the strings.
  • \ r Moves the cursor to the beginning, and “Solution” carries eight characters. The first eight characters are deleted, and the solution is printed.
  • You can check the output for better understanding.
  1. Using Carriage Returns in Python with New Line Characters:

The string program uses “\ r” with a new line character (\ n) in this example.

 
String = 'My Website Lateral \ r \ n Solution.'
String = 'My Website Lateral \ n \ r Solution.'
anyvar = 'My Web \ n SiteLatracle \ r Solution.'

Output:

  • My website is Lateracle
  • Solution
  • My website is Lateracle
  • Solution
  • My web
  • Solution Lateral

Description:

  • First, I got the input string like a string.
  • Then, I applied \ n and \ r to multiple places in the string.
  • \ n For row breaks.
  • The first two strings have \ r before and after \ r. Therefore, the output is output with a line break.
  • In the last string, \ n has been changed because it comes first after “sites” and contains eight characters like a solution.
  • So, you can see the output.

Read More: 3 Best Ways to Exit a Python Program

The Python carriage return (\r) is a special character that tells the Python interpreter to start anew at the beginning of the line. This means that if you’re using \r to create a new line, anything after the \r will be ignored by Python.

To use carriage return in your Python code, simply add the \r escape character wherever you want to start a new line.

For example:


print("This is my first line\r")
print("This is my second line")

Output

This is my first line # Notice how the second print statement is ignored!

This is my second line

LEAVE A REPLY

Please enter your comment!
Please enter your name here