Open In App

difference between newline and carriage return in python

Last Updated : 27 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The newline and carriage return are two special characters used to control text’s display and formatting. Although they may appear similar they have distinct functionalities & serve different purposes. This article aims to provide a clear understanding of the difference between newline & carriage return in Python.

  • The newline character is represented by “\n” & it is used to create a new line in the string or file.
  • The carriage return character represented by “\r” moves the cursor to the beginning of the current line without advancing to the next line.

Newline in Python

The newline character (\n) is a unique character used to represent a line break in a string or a text file. When encountered in a string or used in output operations. it instructs the program to start a new line. The newline character is essential for formatting text and creating multi-line strings.

Syntax

print(“—-\—-“)

Example 1

The code in this line is a string. Its argument passed to the “print()” function. “\n” within the string is an escape sequence that represents a new line. So it ends that line and starts a new one, then it prints “World”. So now The end result is a given separated line.

Python




print("Hello\nWorld")


Output 

Hello
World

Example 2

In this code, the text within the parentheses is a string. ‘\n’ within a string is a special character sequence called an escape sequence. It generally represents a “new line” which shows Python starts with a new line.

Python




print("Hi\nhello")


Output

Hi
hello

Carriage return in Python

The carriage return character (\r) is a special control character that originated from the early days of typewriters and teletype machines. It represents the action of moving the cursor or print head back to the beginning of the current line. When encountered in a string or used in output operations.

  • The carriage return is represented by the escape sequence ‘\r ‘.
  • When the carriage return character is encountered in a string it moves the cursor to the beginning of the current line.

Syntax

print(“—-\r—–“)

Example 1

This line “Hello\rWorld” is a string argument. “/r” within a string is an escape sequence that represents a carriage return. A carriage return tells Python to move the cursor back to the beginning of the current line. So, when you execute this line, Python first prints “Hello”, then when it encounters \r, it moves the cursor back to the beginning of the line and continues printing. It then prints “World”.

Python




print("Hello\rWorld")


Output 

HelloWorld

Example 2

In this line “Hi\rhello” is a string. “\r” within a string is an escape sequence that represents a carriage return. A carriage return tells Python to move the cursor back to the beginning of the current line. So, when you execute this line, Python first prints “Hi”, then when it encounters \r, it moves the cursor back to the beginning of the line and continues printing. It then prints “hello”.

Python




print("Hi\rhello")


Output

Hihello

Difference between Newline Character and Carriage Return

  Newline (\n) Carriage return (\r)
Meaning The Signifies the end of the line of text and the beginning of a new one HelloWorld beginning HelloWorld of the current line
Usage They are Commonly used in the text files and when printing output to the console to create new lines Less commonly used, typically only necessary in specific text formats for overwriting or modifying existing text
Syntax  The Represented using escape character sequence \n The Represented using escape character sequence \r
Example

print(“Hello\nWorld”) 

outputs:        

Hello

World

print(“Hello\rworld”) 

outputs: HelloWorld

FAQs on newline and carriage return in Python

 Q1. What is a newline character in Python?

A newline character (\n) is a special escape sequence in Python that is used to indicate the end of a line of text causing the following text to appear on a new line.

Q2. How do I use a newline character to print text on separate lines?

You can use the newline character in a string to print text on separate lines.

Q3. What is a carriage return character (\r) in Python?

A carriage return character is another special escape sequence in Python used to move the cursor to the beginning of the line.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads