DEV Community

oladejo abdullahi
oladejo abdullahi

Posted on

Escape character in python

Escape characters
The backslash, \, is used to get certain special characters, called escape characters, into your string.
There are a variety of escape characters, and here I am going to show you the most useful ones:
• \n the newline character. It is used to move text to the next line. Here is an example:

print('Hi\neveryone!')
Enter fullscreen mode Exit fullscreen mode

Result

Hi
There!
Enter fullscreen mode Exit fullscreen mode

another example

print('did you\nunderstand\n\nwhat happen\n\n\nhere')
Enter fullscreen mode Exit fullscreen mode

Result

did you
understand

what happen


here
Enter fullscreen mode Exit fullscreen mode

looking at the example above and its result you will notice that the gap between each line is different. did you notice that? Yeah! that is because we put different newline character between the sentence in our code. check the code again did you see the "\n" use once twice and three times between the sentence? so, the more you add the character the more it ommit the next line. "\n" ommit just next line, "\n\n" ommited next two line "\n\n\n\" ommited next three line etc.

• \' this is used to insert apostrophes into strings. assuming you have the following string:

myWord= 'I can't commit error'
print(myWord)
Enter fullscreen mode Exit fullscreen mode

This will produce an error because the apostrophe will actually end the string. You can use \' to get around this:

myWord = 'I can\'t commit error'
print(myWord)
Enter fullscreen mode Exit fullscreen mode

Result

I can't commit error
Enter fullscreen mode Exit fullscreen mode

Another option is to use double quotes for the string :

myWord="I can't commit error"
print(myWord)
Enter fullscreen mode Exit fullscreen mode

Result

I can't commit error
Enter fullscreen mode Exit fullscreen mode

• \" is similar to \' i.e you can use double quote instead of single quote for example

myWord='I can\"t commit error'
print(myWord) 
Enter fullscreen mode Exit fullscreen mode

• \ This is used to get the backslash itself. For example:

newline = '\\n is used for newline'
print(newline)
Enter fullscreen mode Exit fullscreen mode

Result

\n is used for newline
Enter fullscreen mode Exit fullscreen mode

run the code. did you see the backslash? yeah! despite the fact that "\n" it self is used for newline and it is inside our code above yet it doesn't move to next line because the two backslash is interpreted and n is consider as text.

• \t the tab character this is used to give tab space.
example

tab_word="I \twas \tgiven \ttab"
print(tab_word)
Enter fullscreen mode Exit fullscreen mode

Results

I     was     given     tab
Enter fullscreen mode Exit fullscreen mode

try to confirm the code with the result did you see the tab space between them. try more code
Codes

mixing_tab= "I \twas \t\tgiven \t\t\tdifferent \t\t\ttab"
print(mixing_tab)
Enter fullscreen mode Exit fullscreen mode

Result

I     was         given             different             tab
Enter fullscreen mode Exit fullscreen mode

there are more special character which we are going to discuss later but practice this for now and enjoy!

Top comments (0)