Python Escape chracters

Kenan YAMAN
10 min readJan 16, 2021

If you’re engaged in programming and interested, you’ve seen or heard escape signs or escape sequences from somewhere. You’re very likely to make this mistake at the end of many of the busiest hours and shifts of many projects!!! As with many reasons for this, the most common and overlooked problem is the forgetfulness of escape signs. A lot of people who are new to programming in particular often encounter it.

What are these escape sequences? When we want to use the signs or characters in python seductive, which means special meaning, we encounter these escape sequences.

For example, when we want to define a string in python, we can do it in three different ways. Using single quotes double nails or three nails. Let’s think of a scenario like this, we have a character index, and we need to use one nail here.

'This is Sakarya'

If we use it this way, python will form the character index after the first nail and finish the character index when it reaches the quotation mark at the end of the word Sakarya. The rest of the “dir” supplement will give syntax error because it does not know what it means.

In order to avoid this and a lot of other mistakes, we need to know the escape sequences. Let’s get started :)

Reverse Taksim ()

Now that you’ve researched these issues and come to this page, I think you know how to define strings in python. We can use three different methods, as I mentioned above when defining strings. I want to continue with the example I gave you above. When you define a string like this:

"This is Sakarya".

We know we’re going to get a mistake. We said there were three different ways to define strings, so can’t we just define a string?

"This is Sakarya"

We can do it and we don’t deal with any syntax error errors.

We can even make a description like this:

""This is Sakarya""

We do not encounter any errors with this definition, but when we have to define a string with one fingernail, double quoteor three quotes, our help is running a reverse taksim sign :)

For this, using the reverse cab before the quotation mark pythona, it does not end the string with the quotation mark, it gives the message there is more to the continuation. And when the python sees this, he’s looking for a second nail mark that will continue to run and finish the string. When he finds the quotation mark, he finishes the string identification. Let’s do an impression right away on the same sample

'This is Sakarya''

When we type the code in this way, we will not encounter any errors.

The same code applies in double quotes and three quotes.

But where are we going to need this? Let’s think of a scenario, you want to send a message to the user, you can use the reverse taksim sign. You have read a character index from within a text file, and you will process it on the character index, but you cannot interfere with it, which is in case python will error if you want to use the character sequence you received. You can edit it and use reverse division before quotation marks.

Line per Line (n)

n character sequence is a special character that is to switch to a subline in a python. We use this special character between long-line text to go downto the bottom line at any time and keep the program running.

But the thing is, if this particular character is in a description and we want to use that description, it can cause unexpected output. Therefore, it is one of the things that should be considered in particular.

Let’s think of a scenario like this, let’s put the path to a file on our computer, but if a “n” character comes up to the reverse taksim sign in this file path, we’ll be involuntarily using another special set of characters for python. So python is not the output we want, but whatever special character we’re using at that moment, it’s going to function. Let’s make an example now:

'C:pulsepatients.txt'

When we make such a description, when the python n character (pulse) goes to the part where it is, it switches to a bottom line and continues to function. In fact, what we wanted to do was put the path to a file on our computer on the screen.

In this case, if we do not know that the character sequence n has a special meaning, it will be inevitable that the applications we write will yield unexpected results.

Let’s think of a scenario in this. For example, you want to open a file on your computer with an open() function, and you have a nod as I sample duped as a file path. In this case, the program you typed will not work. And it’ll cause it to collapse.

To prevent this, you can double use the reverse cab marks in the directory. Thus, it will not cause any mistakes.

Double reverse taxi use

Tab (t)

Just like in the character “n”, if the reverse taksim sign comes before the letter “t”, a special character appears in the sense of tab for python.

For example, we use the t character index to leave a tably space in text.

Creates four spaces on the screen as if once pressed on the tab tab

It works for us when we use this escape sign on purpose, but it negatively affects the functioning of the practice we write when we miss it.

Let’s continue using the example we use in the n custom character sequence.

'C:pulseblood pressurepatients.txt'

when we do a definition in the form of a definition, we encounter a result like this.

t escape sign example

n progresses to the python functioning by reading codes from left to right, as in a special character sequence, the reverse split sign twice to let the n character sequence know that we are using it for a different purpose, not to actually switch to a new line We write, we continue to read the codes, when you see the t sign, interpret that it is written for a new tab, leaving a tab space, presses the rest of the post to the screen.

To prevent this, we can use a double reverse division, just like we used in the n escape directory. This allows us to let the python know that this is not actually a tab mark

t escape sign example

Ringtone (a)

A special character index formed by the inverse split sign when another letter is next to each other is the letter “a”. When these two characters come together, they cause beep.

You might want to try this. This does not work on linux systems. It does not work on every windows system. It causes such an effect in some versions.

Don’t forget to consider this when working with Strings.

Same Line per Line (r)

This letter, which is critical in our alphabet and often we use in sentences, consists of a line-by-line escape sequence with the side-by-side of the reverse division.

When this escape sequence runs, it writes the characters after where it was located on the entire character written before its location. Let’s make an example right away.

"I'm learning python"

r escape directory example

Python begins interpreting the code that is written from the left, returns to the beginning of the line after the “python” article, and writes the word “I’m learning.” Let’s make another example.

"I'm learningrpython"

Python reinterprets starting from the left, when it comes to the “r” escape directory, writes it at the beginning of the line as it is when the word comes after the escape directory, and adds more characters left over from the first word. The word “I’m learning” consists of 11 characters, the word “python” consists of 6 characters, so the word “I’m learning” is written in six characters, and the remaining four characters are added to the word “python”. We came across this strange word as it came out of “pythonming” :)

Cursor Scrolling (b)

The task of this escape sequence is to shift the cursor to the left of a character. Let’s make an example right now.

"Kenan yaman"

b escape directory

Under normal circumstances, I used the “b” escape sequence after a gap and space between the words “Kenan” and “egregious”. The result is a contiguous version of the two words. When this code came to the escape directory while interpreting it, a character shifted the cursor to the left and gave the screen an output like this.

Let’s write the same sample without a space.

b escape directory example

This time, when it came to the escape directory, the cursor slipped to the left of a character and wrote it over the letter “n.” So a character has been deleted.

You can scroll to the left as many cursors as you want by typing this escape directory side by side many times.

Little Unicode (u)

We’re on a dangerous escape sequence. First of all, let’s define what unicode is

Unicode: A system in which all characters you see on a computer screen are uniquely defined by custom numbers (letters, numbers, numbers, special characters). We like nuptify this, every device with an internet output has a unique mac address. In this system, codes that are unique and custom-made for each character like it.

You can look at this link for Latin letters and see this link for other letters and symbols

Python looks for a unicode character that you see side by side with the reverse division and “u” characters. He makes an error when he can’t find it. If we miss this in the apps we write, we could cause the program to crash. Let’s set an example.

File location: C:usersdocumentspython.pdf

When we want to output the screen like this, the python interpreter reads the code starting from the left and looks for a unicode in subsequent characters when the escape sequence arrives. If he can’t find it, it’ll cause an error.

As with other escape sequences, you can pair the reverse taksim sign to overcome this problem.

Great Unicode (U)

The capitalized escape sequence means the same meaning as any other escape sequence written in the same lowercase letter. The only difference between them is that the lower unicode holds a 4-character field, while the capitalletter-typed unicode holds an eight-character field.

To type the character “i”, we need to write a code with little unicode.

'u0131'

The big unicode uses this to write the same character.

'U00000131'

Long Name (N)

Another escape sequence related to Unicode is the escape series written with the letter “N”.

A unique long “name” is also used in the Unicode system as well as a unique code. The unique long name used for the letter “a” is as follows “LATIN SMALL LETTER A”

You can look at the long names of all letters via the link I give above.

As with other unicode escape sequences, python will make an error because it cannot find a response if it does not see any long name unicode identification when it comes to the escape directory.

Hexadecimal Character (x)

As with other escape sequences, when the reverse division and the letter “x” come together, it produces an escape sequence for the hexadecimal counting system.

In the hexadecimal counting system, as in unicode, each letter and number have a equivalent. We can see this with a code like this.

The “x6b” of the hexadecimal system is used for the letter “k”. You can use this site to look at all characters and symbols.

Finally, I want to show you how we can get rid of these escape sequences in a more practical way.

If an escape sequence falls like this in a string, you put the letter “r” at the beginning of the string, python will ignore these escape sequences. Let’s set an example.

print(r'C:pulseblood pressurepatients.txt')

In normal circumstances, when we wanted to make an example like the above, we would have inadvertently used the n and t escape sequences, but we neutralized these escape sequences with a single letter “r” that we used before the quotation mark.

Disable escape sequences

Our good understanding of escape sequences is important for the health of the programs we will write. You never know where or when these characters will appear.

We’re at the end of the escape sequences. Hope you write code without errors and problems…. :)

Photo by Hitesh Choudhary on Unsplash

This article was first published on the website www.kenanyaman.com on 26.12.2019

--

--