Python, r ý nghĩa

con trăn

lập trình

r

lá cờ

trước

sợi dây

VN

Python - 'r' trước chuỗi nghĩa là gì?

1 câu trả lời

0 điểm

Được hỏi bởi

Haris65

476

s = r'lang\tver\nPython\t3' print[s]

Code language: Python [python]
7 trước một chuỗi trong Python là gì?

Thí dụ

text = r' \1'

0 nhận xét Thêm nhận xét

Tạo câu trả lời 1 câu trả lời

0 điểm

trả lời bởi

Haris65

476

Có nghĩa là chuỗi sẽ được coi là chuỗi thô

Bạn có thể đọc về chuỗi thô, trong tài liệu Python

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.

0 nhận xét Thêm nhận xét

Tạo câu trả lời

0 điểm

trả lời bởi

Haris65

476

Có nghĩa là chuỗi sẽ được coi là chuỗi thô

Bạn có thể đọc về chuỗi thô, trong tài liệu Python

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.

0 nhận xét Thêm nhận xét

Bản tóm tắt. trong hướng dẫn này, bạn sẽ tìm hiểu về các chuỗi thô Python và cách sử dụng chúng để xử lý các chuỗi coi dấu gạch chéo ngược là ký tự chữ

Giới thiệu về chuỗi thô Python

Trong Python, khi bạn đặt trước một chuỗi bằng chữ cái

s = r'lang\tver\nPython\t3' print[s]

Code language: Python [python]
0 hoặc

s = r'lang\tver\nPython\t3' print[s]

Code language: Python [python]
1, chẳng hạn như

s = r'lang\tver\nPython\t3' print[s]

Code language: Python [python]
2 và

s = r'lang\tver\nPython\t3' print[s]

Code language: Python [python]
3, chuỗi đó sẽ trở thành chuỗi thô. Không giống như một chuỗi thông thường, một chuỗi thô coi các dấu gạch chéo ngược [

s = r'lang\tver\nPython\t3' print[s]

Code language: Python [python]
4] là các ký tự bằng chữ

Các chuỗi thô rất hữu ích khi bạn xử lý các chuỗi có nhiều dấu gạch chéo ngược, ví dụ: biểu thức chính quy hoặc đường dẫn thư mục trên Windows

Để đại diện cho các ký tự đặc biệt như tab và dòng mới, Python sử dụng dấu gạch chéo ngược [

s = r'lang\tver\nPython\t3' print[s]

Code language: Python [python]
4] để biểu thị sự bắt đầu của chuỗi thoát. Ví dụ

s = 'lang\tver\nPython\t3' print[s]

Code language: Python [python]

đầu ra

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
0

Tuy nhiên, các chuỗi thô xử lý dấu gạch chéo ngược [

s = r'lang\tver\nPython\t3' print[s]

Code language: Python [python]
4] như một ký tự chữ. Ví dụ

s = r'lang\tver\nPython\t3' print[s]

Code language: Python [python]

đầu ra

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
3

Một chuỗi thô giống như chuỗi thông thường của nó với dấu gạch chéo ngược [_______34] được biểu thị dưới dạng dấu gạch chéo ngược kép [

s = r'lang\tver\nPython\t3' print[s]

Code language: Python [python]
8]

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
6

Trong một chuỗi thông thường, Python tính một chuỗi thoát là một ký tự đơn

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
7

Tuy nhiên, trong một chuỗi thô, Python đếm dấu gạch chéo ngược [_______34] là một ký tự

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
9

Vì dấu gạch chéo ngược [

s = r'lang\tver\nPython\t3' print[s]

Code language: Python [python]
4] thoát khỏi dấu nháy đơn [
When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
31] hoặc dấu ngoặc kép [
When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
32], một chuỗi thô không thể kết thúc bằng số lẻ dấu gạch chéo ngược

Ví dụ

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
3

Lỗi

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
4

Hoặc

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
5

Lỗi

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
4

Sử dụng chuỗi thô để xử lý đường dẫn tệp trên Windows

Hệ điều hành Windows sử dụng dấu gạch chéo ngược để phân tách các đường dẫn. Ví dụ

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
01

Nếu bạn sử dụng đường dẫn này như một chuỗi thông thường, Python sẽ phát sinh một số lỗi

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
02

Lỗi

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
03

Python coi \u trong đường dẫn là một lối thoát Unicode nhưng không thể giải mã nó

Bây giờ, nếu bạn thoát khỏi dấu gạch chéo ngược đầu tiên, bạn sẽ gặp các sự cố khác

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
04

đầu ra

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
05

Trong ví dụ này,

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
33 là một tab và
When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
34 là dòng mới

Để dễ dàng, bạn có thể biến đường dẫn thành một chuỗi thô như thế này

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
06

Chuyển đổi một chuỗi thông thường thành một chuỗi thô

Để chuyển đổi một chuỗi thông thường thành một chuỗi thô, bạn sử dụng hàm repr[] tích hợp sẵn. Ví dụ

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
07

đầu ra

When an 'r' or 'R' prefix is present, a character following a backslash is included in 
the string without change, and all backslashes are left in the string. For example, the string 
literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes 
can be escaped with a backslash, but the backslash remains in the string; 
for example, r"\"" is a valid string literal consisting of two characters: 
a backslash and a double quote; r"\" is not a valid string literal 
[even a raw string cannot end in an odd number of backslashes]. 
Specifically, a raw string cannot end in a single backslash [since the backslash 
would escape the following quote character]. Note also that a single backslash followed 
by a newline is interpreted as those two characters as part of the string, 
not as a line continuation.
08

Lưu ý rằng chuỗi thô kết quả có trích dẫn ở đầu và cuối chuỗi. Để loại bỏ chúng, bạn có thể sử dụng lát

Chủ Đề