
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Ways to print escape characters in python
In this article, we are going to see how we can print the escape characters in Python. I think you know what escape characters are? Let's see what escape characters for those who don't know are?
Escape characters are used for the individual meanings in strings. If we want to include a new line, tab space, etc., in strings, we can use these escape characters. Let's see some examples.
Example
## new line new_line_string = "Hi\nHow are you?" ## it will print 'Hi' in first line and 'How are you?' in next line print(new_line_string)
Output
If you run the above program, it will produce the following results.
Hi How are you?
We have many escape characters in Python like \n, \t, \r, etc., What if we want to print a string which contains these escape characters? We have to print the string using repr() inbuilt function. It prints the string precisely what we give. Let's see an example.
Example
## string string = "Hi\nHow are you?\nFine." ## exact string will be printed print(repr(string))
Output
If you run the above program, it will produce the following results.
'Hi\nHow are you?\nFine.'
We can also print the exact string without using repr() function. We have initialized string using r or R string trigger. Let's see it with an example.
Example
## string trigger string = R"Hi\nHow are you?\nFine." ## exact string will be printed print(string)
Output
If you execute the above program, it will produce the following results.
Hi\nHow are you?\nFine.
We can also use the small alphabet of R and will get the same result.
Conclusion
I hope you learn how to print the escape characters in Python. If you have any doubts regarding the article, please mention them in the comment section.
- Related Articles
- Ways to print escape characters in C#
- Escape Characters in Python
- Escape characters in JavaScript
- How to escape all special characters for regex in Python?
- Golang Program to demonstrate the escape sequence characters
- How can we escape special characters in MySQL statement?
- Python program to print Possible Words using given characters
- List down a list of the escape characters in C#
- Python program to print design door mat texture using characters
- How to escape characters that can be interpreted as XML markup in JSP?
- Python code to print common characters of two Strings in alphabetical order
- Different ways to print exception messages in Java
- How to print characters from a string starting from 3rd to 5th in Python?
- Program to print maximum number of characters by copy pasting in n steps in Python?
- Escape a Large Maze Python
