SoFunction
Updated on 2025-03-01

A brief analysis of the difference between adding r, f, u, and l before python strings

First, let me introduce to you the meaning of u, r, b, f before the Python string (string prefix)

1. Add u before the string

Example: u "I am a string consisting of Chinese characters."

effect:

The following string is encoded in Unicode format and is generally used in front of Chinese strings to prevent garbled code when used again due to source code storage format problems.

2. Add r before the string

Example: r"\n\n\n\n"# represents a normal new string \n\n\n\n\n, not a new line.

effect:

Remove the transfer mechanism of the backslash.

(Special characters: that is, those with backslashes plus corresponding letters representing the corresponding special meaning, such as the most common "\n" means line breaks, "\t" means Tab, etc.)

application:

Commonly used in regular expressions, corresponding to the re module.

3. Add b before the string

Example: response = b'<h1>Hello World!</h1>'    # b' ' means this is a bytes object

effect:

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

usefulness:

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 both bytes

Attached:

In Python3, the way bytes and str are converted to each other is

('utf-8')
('utf-8')

4. Add f before the string

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

Output:

processing done in 1.00 s

The following introduces the difference between adding r, f, u, and l before python strings

f-strings refers to a string starting with f or F, where expressions contained with {} are replaced with values. (Currently supported python 3.6 version)

Let's take a look at how to use f-strings

Basic use (function: replace value)

&gt;&gt;&gt;name = 'xiaoming'
&gt;&gt;&gt; age = 18
&gt;&gt;&gt; f"hi, {name}, are you {age}"
#The result is as follows'hi, xiaoming, are you 18'
&gt;&gt;&gt; F"hi, {name}, are you {age}"
'hi, xiaoming, are you 18'

Adding r before string prevents string escape

Function: No special or unprintable characters escaped.

>>> s='abc\nabc'
>>> s
'abc\nabc'
>>> print s
abc
abc
>>> s=r'abc\nabc'
>>> s
'abc\\nabc'
>>> print s
abc\nabc

u/U: represents unicode string

It is not just for Chinese, but for any string, which means that the string is unicode encoding.
Generally, English characters can be parsed normally under various encodings, so they generally do not have u; however, in Chinese, it must indicate the required encoding, otherwise garbled code will appear once the encoding is converted. It is recommended that all encoding methods use utf8

Add "l" before the string

Represents wide characters, unicode characters (the unicode character set is composed of two bytes. L tells the compiler to use a two-byte unicode character set). For example, L "My String" means converting an ANSI string into a unicode string, which means that each character takes up two bytes.

Take up bytes without adding time
strlen("asd") = 3; 
 
 Takes up bytes after adding
strlen(L"asd") = 6;

This is the end of this article about briefly analyzing the differences between adding r, f, u, and l before python strings. For more related contents of python strings, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!