0
0
Pythonprogramming~5 mins

Escape characters in output in Python

Choose your learning style9 modes available
Introduction

Escape characters help you show special symbols or control text layout in output. They let you print things like new lines or quotes inside text.

When you want to start a new line in printed text.
When you need to include quotes inside a string without ending it.
When you want to add a tab space to organize output.
When you want to show a backslash character in your text.
When you want to control how text appears on the screen.
Syntax
Python
print("Hello\nWorld")
print("She said, \"Hi!\"")

Escape characters start with a backslash \ followed by a letter or symbol.

Common escape characters: \n (new line), \t (tab), \\ (backslash), \" (double quote), \' (single quote).

Examples
Prints Hello and World on two separate lines using \n for new line.
Python
print("Hello\nWorld")
Prints quotes inside the string by escaping double quotes.
Python
print("She said, \"Hello!\"")
Prints a tab space between words using \t.
Python
print("Column1\tColumn2")
Prints a single backslash by escaping it with another backslash.
Python
print("This is a backslash: \\")
Sample Program

This program shows different escape characters in action: new line, quotes, tab, and backslash.

Python
print("Line1\nLine2")
print("Quote: \"Python\"")
print("Tab\tSpace")
print("Backslash: \\")
OutputSuccess
Important Notes

Remember to use escape characters inside quotes to avoid errors.

Raw strings (prefix with r) ignore escape characters, useful for file paths.

Summary

Escape characters let you control special text output.

Use \n for new lines, \t for tabs, and \\ for backslash.

Escape quotes inside strings to print them correctly.