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 is the difference between r and u in a Python string

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what is the difference between r and u in Python string". In daily operation, I believe many people have doubts about what is the difference between r and u in Python string. Xiaobian consulted all kinds of information and sorted out simple and easy operation methods. I hope to help you answer the question of "what is the difference between r and u in Python string"! Next, please follow the small series to learn together!

We often see r(R) or u/(U) prefixes before strings in Python. What do these two symbols mean?

1.r(R)

r stands for raw and represents a native string that contains no escape characters. Common escape characters include the following:

Escape Character Description\(at the end of a line) Endurance\\Backslash Symbol 'Single Quotation (string needs to be of the form')'Double Quotation (string needs to be of the form')\b Backspace\000 Empty\n Newline\v Vertical Tab\t Horizontal Tab

Here are a few examples of escape characters:

print("Hello\,world")# Hello,worldprint("Hello\\,world")# Hello\,worldprint("Hello',world")# Hello',worldprint('Hello",world')# Hello",worldprint("Hello\b,world")# Hell,worldprint("Hello\000,world")#Hello,worldprint("Hello\n,world")# Hello# ,worldprint("Hello\v,world")# Hello# ,worldprint("Hello\t,world")# Hello ,worldprint("Hello\020,world")

The r prefix tells the interpreter that this string does not contain escape characters, such as '\n' in the string, it is not treated as a newline character, but as a '\' character and 'n' character. As shown below:

print("Hello,\nworld")# Hello,# worldprint("Hello,\nworld")# Hello,\nworld

The most common use of the r prefix is for regular expressions, because regular matching patterns often contain characters such as backslashes, and we don't want it to be resolved as a transition character, so we need to add 'r'.

as

import restr_pat= re.compile(r'\d+/\d+/\d+')text = 'Today is 12/10/2021, yesterday is 12/11/2021'res = str_pat.findall(text)print(res)['12/10/2021', '12/11/2021']2.u(U)

The u(U) prefix indicates that the encoding of a string is unicode. Any character string, including not only Chinese, can be unicode encoded. English strings can be parsed normally in any encoding, so you generally don't need to explicitly add u. However, Chinese had better explain its encoding, otherwise the encoding conversion will appear garbled (such as originally using gbk encoding but using unicode to decode). The solution to the coding problem once and for all is to add the following to the header of the.py file:

# coding: utf-8 supplement

String preceded by b

Response = b'Hello World! ' # b' means this is a bytes object

Function:

The prefix b" " indicates that the following string is of type bytes.

Usage:

In network programming, servers and browsers only recognize bytes type data.

For example, the parameters of the send function and the return value of the recv function are of type bytes.

Attached:

In Python 3, bytes and str are converted to each other

str.encode('utf-8')bytes.decode('utf-8')

String preceded by f

import timer 0 = time.time()time.sleep(1)name = 'processing'#Start with f to indicate that python expressions in braces are supported within strings print(f'{name} done in {time.time() - t0:.2f} s')

Output:

processing done in 1.00 s

At this point, the study of "what is the difference between r and u in Python strings" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more 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: 0

*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