Today, We want to share with you escape sequence in python.In this post we will show you escape character for space python, hear for python escape backslash we will give you demo and example for implement.In this post, we will learn about PHP Addslashes Function With Examples with an example.
Python 3 Escape Sequences
There are the following the List of escape sequences available in Python 3. The below table contains a list of Python Escape sequence characters and relevant examples.
Example 1: Escape Sequence Example
$ python Python 2.7.5 (default, Jun 25 2021, 00:41:19) [GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print u"\u041b" Л
Example 2:
txt = "I am the so-called \"Pakainfo\" from the india." print(txt)
I am the so-called "Pakainfo" from the india.
You can run all below examples from python prompt.
Escape Sequence | Description | Example |
---|---|---|
\newline |
Backslash and newline ignored |
print("line1 \
line2 \
line3")
Result
line1 line2 line3 |
\\ |
Backslash (
\ ) |
xxxxxxxxxx
print("\\")
Result
\ |
\' |
Single quote (
' ) |
xxxxxxxxxx
print('\'')
Result
' |
\" |
Double quote (
" ) |
xxxxxxxxxx
print("\"")
Result
" |
\a |
ASCII Bell (BEL) |
xxxxxxxxxx
print("\a")
|
\b |
ASCII Backspace (BS) |
xxxxxxxxxx
print("Welcome \b Pakainfo!")
Result
Welcome Pakainfo! |
\f |
ASCII Formfeed (FF) |
xxxxxxxxxx
print("Welcome \f Pakainfo!")
Result
Welcome Pakainfo! |
\n |
ASCII Linefeed (LF) |
xxxxxxxxxx
print("Welcome \n Pakainfo!")
Result
Welcome Pakainfo! |
\r |
ASCII Carriage Return (CR) |
xxxxxxxxxx
print("Welcome \r Pakainfo!")
Result
Welcome Pakainfo! |
\t |
ASCII Horizontal Tab (TAB) |
xxxxxxxxxx
print("Welcome \t Pakainfo!")
Result
Welcome Pakainfo! |
\v |
ASCII Vertical Tab (VT) |
xxxxxxxxxx
print("Welcome \v Pakainfo!")
Result
Welcome Pakainfo! |
\ooo |
Character with octal value ooo |
xxxxxxxxxx
print("\110\145\154\154\157\40\127\157\162\154\144\41")
Result
Welcome Pakainfo! |
\xhh |
Character with hex value hh |
xxxxxxxxxx
print("\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21")
Result
Welcome Pakainfo! |
Some list of escape sequences are only recognized in string literals.
These are:
Escape Sequence | Description |
---|---|
\N{name} |
Character named name in the Unicode database |
\uxxxx |
Character with 16-bit hex value xxxx. Exactly four hexadecimal digits are required. |
\Uxxxxxxxx |
Character with 32-bit hex value xxxxxxxx. Exactly eight hexadecimal digits are required. |
List of Python Escape sequence characters with examples
Escape Sequence | Description | Example | Output |
---|---|---|---|
\\ | Prints Backslash | print “\\” | \ |
\` | Prints single-quote | print “\'” | ‘ |
\” | Pirnts double quote | print “\”” | “ |
\a | ASCII bell makes ringing the bell alert sounds ( eg. xterm ) | print “\a” | N/A |
\b | ASCII backspace ( BS ) removes previous character | print “ab” + “\b” + “c” | ac |
\f | ASCII formfeed ( FF ) | print “welcome\fpakainfo” | welcome pakainfo |
\n | ASCII linefeed ( LF ) | print “welcome\npakainfo” | welcome pakainfo |
\N{name} | Prints a character from the Unicode database | print u”\N{DAGGER}” | † |
\r | ASCII carriage return (CR). Moves all characters after ( CR ) the the beginning of the line while overriding same number of characters moved. | print “123456\rXX_XX” | XX_XX6 |
\t | ASCII horizontal tab (TAB). Prints TAB | print “\t* welcome” | * welcome |
\t | ASCII vertical tab (VT). | N/A | N/A |
\uxxxx | Prints 16-bit hex value Unicode character | print u”\u041b” | Л |
\Uxxxxxxxx | Prints 32-bit hex value Unicode character | print u”\U000001a9″ | Ʃ |
\ooo | Prints character based on its octal value | print “\043” | # |
\xhh | Prints character based on its hex value | print “\x23” | # |
LinuxConfig.org |
Python Escape Sequence – Practice Exercises
#Exercises 1
print("PakainfoCOM is an \"awesome\" website.")
#Exercises 2
print("PakainfoCOM\n\t2020")
#Exercises 3
print('I\'m from PakainfoCOM.\b')
#Exercises 4
print("\65")
#Exercises 5
print("\x65")
#Exercises 6 print("PakainfoCOM", "2020", sep="\n")
#Exercises 7
print("PakainfoCOM", "2020", sep="\b")
#Exercises 8
print("PakainfoCOM", "2020", sep="*", end="\b\b\b\b")
I hope you get an idea about Python Escape Characters.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.