Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Python escape representations

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "what Python escape notation". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

1. Why escape?

There are 128 characters in the ASCII table. There are letters, numbers, punctuation marks that we are very familiar with, which can be output from our keyboard. There are also some very special characters that I would normally find hard to find on a keyboard, like tabs and bells.

In order to be able to write those special characters into the string variable, it specifies a character for escape\. With this character, the characters you see in the string may not be what you originally saw after printing out.

for example

>>> msg = "hello\013world\013hello\013python" >>> print(msg) hello world hello python >>>

Isn't that a bit magical? It's becoming a stepped output.

What does that mean?

\is an escape symbol, as mentioned above.

013 is ASCII coded octal representation, note that it is preceded by 0 and cannot be omitted, not the letter o

If you convert octal 13 to decimal, it's 11.

Looking at the ASCII code table, 11 corresponds to a vertical positioning symbol, which explains why it is a stepped output string.

2. 5 ways to escape

ASCII has 128 characters, and if it is represented in octal, it must have at least three digits to represent all of them. That's why I can't leave out the first zero, even if I don't need it right now.

If you use hexadecimal, as long as the two digits on its ASCII characters are all expressed. At the same time, in order to avoid confusion with octal, the English letter x should be added after\to indicate hexadecimal, followed by two hexadecimal values.

\starts with a three-digit value 0-7, indicating octal

\x begins with two digits 0-f, indicating hexadecimal.

So when I define a string with the value hello + carriage return + world, there are several ways:

#First method: Octal>>> msg = "hello\012world" >>> print(msg) hello world >>> #The second method: hexadecimal>>> msg = "hello\x0aworld" >>> print(msg) hello world >>>

It is often difficult to remember the ASCII number of a character, and even if we do remember it, it is too difficult to convert it to octal or hexadecimal.

Therefore, for some common and special characters, we are used to using another alias-like way, such as using\n to represent newlines, which is equivalent to\012 and\x0a.

Similar representations, as well as the following

So, to do hello + carriage return + world, there's a third way.

#Third method: Use alias like method>>> msg = "hello\nworld" >>> print(msg) hello world >>>

So far, we have mastered three expressions of escape.

It's already very rare, let's make our brain hole bigger, and then introduce two more.

ASCII code table can express characters is too limited, want to print a Chinese character, sorry, you have to use Unicode code.

Unicode encoding is composed of four hexadecimal values

>>> print("\u4E2D")

What? Why do I know unicode is\u4E2D? Print it like this and you'll know

# Python 2.7 >>> a = u"in" >>> a u'\u4e2d'

Thus, to achieve hello + carriage return + world, there is a fourth way.

#Fourth method: use unicode,\u000a for newline>> print ('hello\u000aworld') hello world

Do you think this is over?

No, not yet. There's another one below.

Unicode encoding can also be composed of eight 32-ary values, which start with\U in order to distinguish them from the previous ones.

#The fifth method: use unicode,\U000000A for newline>> print ('hello\U00000Aworld') hello world

Okay, so far we have five escape expressions.

To sum up:

\starts with a three-digit number 0-7 (octal) ---can represent all ASCII characters

\x starts with two digits 0-f (hexadecimal) ---can represent all ASCII characters

\u starts with four digits 0-f (hexadecimal) ---can represent all Unicode characters

\U starts with an octet 0-f (base 32)---can represent all Unicode characters

\begins with a specific character other than x, u, U---only some characters can be represented

Why does the title say that escape can also show off skills?

Imagine if your colleague used this unicode code when printing logs, and then you used this keyword to search when locating the problem, only to find nothing? This was the end of the road.

Although this behavior is really sb, it may seem to some people to be a very powerful operation.

The five escape expressions are introduced here, followed by more escape-related content, but also very interesting content, interested can continue to look down.

3. raw strings

When we print a string with escaped characters, normally the output is not what we saw in the string.

What if we need to print hello\nworld and don't want Python to escape\n as a newline?

In this case, you can define the string as a raw string by adding an r or R to the string.

>>> print(r"hello\nworld") hello\nworld >>> >>> print(R"hello\nworld") hello\nworld

However, not all times you can add r, such as when your string is returned to you by a program/function, rather than being generated by yourself.

#Assuming this is foreign data, return "hello\nworld" >> body = spider() >>> print(body) hello world

This time print it,\n is newline print.

4. Use Repr

For situations where r cannot be used, try repr to solve this requirement:

>>> body = repr(spider()) >>> print(body) 'hello\nworld'

After the repr function is processed, it actually does two things to make the result after printing look like the string itself.

HarmonyOS Technology Community

changed\to\\

Add 'or' at the beginning and end of the string

You can type the variable Enter under Python Shell, and you can see the clue.

Whether you add 'or' depends on your original string.

>>> body="hello\nworld" >>> repr(body) "'hello\\nworld'" >>> >>> >>> body='hello\nworld' >>> repr(body) "'hello\\nworld'"

5. Use string_escape

If you're still using Python 2, there's another way to do it.

That's using string.encode ('string_escape'), which also works like repr

>>> "hello\nworld".encode('string_escape') 'hello\\nworld' >>>

6. View native strings

To sum up, there are two ways to view native strings:

HarmonyOS Technology Community

If you are in Python Shell interactive mode, type Enter.

If you are not in Python Shell interactive mode, you can use repr to process it first, and then use print to print.

>>> body="hello\nworld" >>> >>> body 'hello\nworld' >>> >>> print(repr(body)) 'hello\nworld' >>>

7. Recovery escape: convert to original string

After repr processing or\\unescaped strings, is there any way to go back and become the original escaped strings?

The answer is yes.

If you use Python 2, you can do this:

>>> body="hello\\nworld" >>> >>> body 'hello\\nworld' >>> >>> body.decode('string_escape') 'hello\nworld' >>>

If you use Python 3, you can do this:

>>> body="hello\\nworld" >>> >>> body 'hello\\nworld' >>> >>> bytes(body, "utf-8").decode("unicode_escape") 'hello\nworld' >>>

What? What is Python 2 and Python 3? It's too much trouble.

Mingo teaches you to use a script that is compatible with Python 2 and Python 3.

The first is output in Python 2

>>> import codecs >>> body="hello\\nworld" >>> >>> codecs.decode(body, 'unicode_escape') u'hello\nworld' >>>

Then look at the output in Python 3.

>>> import codecs >>> body="hello\\nworld" >>> >>> codecs.decode(body, 'unicode_escape') 'hello\nworld' >>>

You can see that Pyhton 2 output has a u, while Python 3 output has no u, but they are unescaped anyway.

"What Python escape notation" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 293

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report