Python tìm dấu ngoặc kép trong chuỗi

Để trích dẫn một chuỗi trong Python, hãy sử dụng dấu ngoặc đơn bên trong dấu ngoặc kép hoặc ngược lại

Ví dụ

example1 = "He said 'See ya' and closed the door."
example2 = 'They said "We will miss you" as he left.'

print(example1)
print(example2)

đầu ra

He said 'See ya' and closed the door.
They said "We will miss you" as he left.

Chuỗi Python

Chuỗi Python là chuỗi ký tự và số

Một chuỗi được bọc xung quanh một tập hợp các dấu nháy đơn hoặc dấu nháy kép. Không có sự khác biệt trong đó bạn sử dụng

Bất cứ thứ gì nằm trong dấu ngoặc kép đều được hiểu là "văn bản" thay vì một lệnh thực thi

Để chứng minh, đây là một số ví dụ

print("10 + 20")                  # Prints: 10 + 20
print("This # is not a comment")  # Prints: This # is not a comment
print("pow(2,3)")                 # Prints: pow(2, 3)
Trong mỗi ví dụ, có một thao tác Python thường thực thi. Nhưng vì biểu thức được bọc bên trong một chuỗi, nên biểu thức được in ra nguyên trạng

Nhưng đây là nơi nó trở nên thú vị. Hãy xem điều gì sẽ xảy ra khi bạn đặt một dấu ngoặc kép bên trong một chuỗi

print("This "test" causes problems")

Kết quả

  File "example.py", line 1
    print("This "test" causes problems")
                 ^
SyntaxError: invalid syntax

Điều này xảy ra vì trình thông dịch Python nhìn thấy một chuỗi biểu thức gồm ba phần

  1. He said 'See ya' and closed the door.
    They said "We will miss you" as he left.
    3
  2. He said 'See ya' and closed the door.
    They said "We will miss you" as he left.
    4
  3. He said 'See ya' and closed the door.
    They said "We will miss you" as he left.
    5

Nó nhìn thấy hai chuỗi và một tham chiếu đến một đối tượng không tồn tại

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
4. Vì vậy, nó không biết phải làm gì

Để giải quyết vấn đề này, bạn có hai lựa chọn

  1. Sử dụng dấu ngoặc đơn bên trong dấu ngoặc kép (và ngược lại)
  2. Thoát dấu ngoặc kép bên trong chuỗi bằng dấu gạch chéo ngược

1. Dấu ngoặc đơn bên trong Dấu ngoặc kép

Để viết một chuỗi được trích dẫn bên trong một chuỗi khác trong Python

  • Sử dụng dấu ngoặc kép trong chuỗi bên ngoài và dấu ngoặc đơn trong chuỗi bên trong
  • Sử dụng dấu ngoặc đơn trong chuỗi bên ngoài và dấu ngoặc kép trong chuỗi bên trong

Đây là một ví dụ

example1 = "He said 'See ya' and closed the door."
example2 = 'They said "We will miss you" as he left.'

print(example1)
print(example2)

đầu ra

He said 'See ya' and closed the door.
They said "We will miss you" as he left.

Nhưng nếu điều này là không đủ thì sao?

Sau đó, bạn cần phải dùng đến cái được gọi là trình tự thoát. Những thứ này giúp bạn có thể thêm bao nhiêu dấu ngoặc kép vào một chuỗi tùy thích

2. Làm thế nào để thoát khỏi trích dẫn trong một chuỗi

Để thêm các chuỗi được trích dẫn bên trong chuỗi, bạn cần thoát khỏi dấu ngoặc kép. Điều này xảy ra bằng cách đặt dấu gạch chéo ngược (

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
7) trước ký tự thoát

Trong trường hợp này, hãy đặt nó trước bất kỳ dấu ngoặc kép nào bạn muốn thoát

Đây là một ví dụ

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
2

đầu ra

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
3

Cách sử dụng dấu gạch chéo ngược trong chuỗi sau đó

Trong Python, dấu gạch chéo ngược là một ký tự đặc biệt giúp thoát chuỗi

Nhưng điều này cũng có nghĩa là bạn không thể sử dụng nó bình thường trong một chuỗi

Ví dụ

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
4

đầu ra

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
0

Để bao gồm dấu gạch chéo ngược trong một chuỗi, hãy thoát nó bằng dấu gạch chéo ngược khác. Điều này có nghĩa là viết một dấu gạch chéo ngược kép (______18)

Trong Python, một chuỗi (

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
77) được tạo bằng cách đặt văn bản trong dấu nháy đơn
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
78, dấu nháy kép
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
79 và dấu nháy ba (
print("10 + 20")                  # Prints: 10 + 20
print("This # is not a comment")  # Prints: This # is not a comment
print("pow(2,3)")                 # Prints: pow(2, 3)
00,
print("10 + 20")                  # Prints: 10 + 20
print("This # is not a comment")  # Prints: This # is not a comment
print("pow(2,3)")                 # Prints: pow(2, 3)
01). Cũng có thể chuyển đổi các đối tượng thuộc loại khác thành chuỗi với
print("10 + 20")                  # Prints: 10 + 20
print("This # is not a comment")  # Prints: This # is not a comment
print("pow(2,3)")                 # Prints: pow(2, 3)
02

  • Các loại tích hợp - Loại chuỗi văn bản — str — Python 3. 10. 0 tài liệu

Bài viết này mô tả các nội dung sau

  • Dấu nháy đơn.
    He said 'See ya' and closed the door.
    They said "We will miss you" as he left.
    78
  • Dấu ngoặc kép.
    He said 'See ya' and closed the door.
    They said "We will miss you" as he left.
    79
  • Sự khác biệt giữa dấu ngoặc đơn và dấu ngoặc kép
    • Cả hai giá trị đều bằng nhau
    • Dấu ngoặc kép trong chuỗi được xử lý khác nhau
  • dấu ngoặc kép.
    print("10 + 20")                  # Prints: 10 + 20
    print("This # is not a comment")  # Prints: This # is not a comment
    print("pow(2,3)")                 # Prints: pow(2, 3)
    00,
    print("10 + 20")                  # Prints: 10 + 20
    print("This # is not a comment")  # Prints: This # is not a comment
    print("pow(2,3)")                 # Prints: pow(2, 3)
    01
    • Nhiều dòng
    • Dấu nháy đơn và kép
    • thụt đầu dòng
  • Chuyển đổi các loại khác thành chuỗi.
    print("10 + 20")                  # Prints: 10 + 20
    print("This # is not a comment")  # Prints: This # is not a comment
    print("pow(2,3)")                 # Prints: pow(2, 3)
    02
    • Chuyển đổi số thành chuỗi
    • Chuyển đổi danh sách và từ điển thành chuỗi

Liên kết được tài trợ

Dấu nháy đơn. He said 'See ya' and closed the door. They said "We will miss you" as he left.78

Đặt văn bản trong dấu nháy đơn

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
78 để biến nó thành một chuỗi (
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
77)

print("This "test" causes problems")
1

nguồn. str_literal. py

Dấu ngoặc kép. He said 'See ya' and closed the door. They said "We will miss you" as he left.79

Ngoài ra, đặt văn bản trong dấu ngoặc kép

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
79 để biến nó thành một chuỗi (
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
77)

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
7

nguồn. str_literal. py

Sự khác biệt giữa dấu ngoặc đơn và dấu ngoặc kép

Cả hai giá trị đều bằng nhau

Cho dù bạn sử dụng dấu ngoặc đơn

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
78 hay dấu ngoặc kép
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
79, các chuỗi được tạo đều bằng nhau

print("10 + 20")                  # Prints: 10 + 20
print("This # is not a comment")  # Prints: This # is not a comment
print("pow(2,3)")                 # Prints: pow(2, 3)
0

nguồn. str_literal. py

Dấu ngoặc kép trong chuỗi được xử lý khác nhau

Trong một chuỗi được đặt trong dấu nháy đơn

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
78, dấu nháy kép
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
79 có thể được sử dụng nguyên trạng, nhưng dấu nháy đơn
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
78 phải được thoát bằng dấu gạch chéo ngược và được viết là
  File "example.py", line 1
    print("This "test" causes problems")
                 ^
SyntaxError: invalid syntax
29. Không có vấn đề gì nếu bạn viết
  File "example.py", line 1
    print("This "test" causes problems")
                 ^
SyntaxError: invalid syntax
30 cho dấu ngoặc kép
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
79

  File "example.py", line 1
    print("This "test" causes problems")
                 ^
SyntaxError: invalid syntax
2

nguồn. str_literal. py

Trong một chuỗi được đặt trong dấu ngoặc kép

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
79, dấu nháy đơn
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
78 có thể được sử dụng nguyên trạng, nhưng dấu ngoặc kép
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
79 phải được thoát bằng dấu gạch chéo ngược và được viết là
  File "example.py", line 1
    print("This "test" causes problems")
                 ^
SyntaxError: invalid syntax
30. Không có vấn đề gì nếu bạn viết
  File "example.py", line 1
    print("This "test" causes problems")
                 ^
SyntaxError: invalid syntax
29 cho dấu nháy đơn
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
78

  File "example.py", line 1
    print("This "test" causes problems")
                 ^
SyntaxError: invalid syntax
3

nguồn. str_literal. py

Vì nó chỉ là một sự khác biệt trong cách viết, các giá trị trong cả hai trường hợp đều bằng nhau

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
3

nguồn. str_literal. py

Liên kết được tài trợ

dấu ngoặc kép. print("10 + 20") # Prints: 10 + 20 print("This # is not a comment") # Prints: This # is not a comment print("pow(2,3)") # Prints: pow(2, 3)00, print("10 + 20") # Prints: 10 + 20 print("This # is not a comment") # Prints: This # is not a comment print("pow(2,3)") # Prints: pow(2, 3)01

Dấu ngoặc kép (_______200,

print("10 + 20")                  # Prints: 10 + 20
print("This # is not a comment")  # Prints: This # is not a comment
print("pow(2,3)")                 # Prints: pow(2, 3)
01), nghĩa là ba dấu nháy đơn
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
78 hoặc dấu nháy kép
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
79, cũng tạo thành một chuỗi (
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
77)

Nhiều dòng

Xảy ra lỗi khi một dòng mới được chèn vào một chuỗi được đặt trong dấu ngoặc đơn hoặc dấu ngoặc kép. Để chèn một dòng mới, bạn cần sử dụng

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
35

  • Xử lý ngắt dòng (dòng mới) trong Python

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
2

nguồn. str_literal. py

Trong một chuỗi được đặt trong ba dấu ngoặc kép, ngắt dòng có thể được viết như chúng là

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
3

nguồn. str_literal. py

Tất nhiên, nó không nhất thiết phải chứa ngắt dòng

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
4

nguồn. str_literal. py

Dấu nháy đơn và kép

Bạn có thể sử dụng

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
79 trong ba dấu ngoặc đơn
print("10 + 20")                  # Prints: 10 + 20
print("This # is not a comment")  # Prints: This # is not a comment
print("pow(2,3)")                 # Prints: pow(2, 3)
00 và
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
78 trong ba dấu ngoặc kép
print("10 + 20")                  # Prints: 10 + 20
print("This # is not a comment")  # Prints: This # is not a comment
print("pow(2,3)")                 # Prints: pow(2, 3)
01. Giá trị bằng nhau trong cả hai trường hợp

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
9

nguồn. str_literal. py

thụt đầu dòng

Nếu khoảng trắng được chèn vào đầu dòng để khớp với thụt lề, thì chuỗi được tạo bao gồm khoảng trắng

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
70

nguồn. str_literal. py

Có một cách để viết phần sau bằng cách sử dụng nguồn cấp dữ liệu và dấu ngoặc đơn

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
71

nguồn. str_literal. py

Xem chi tiết bài viết sau

  • Xử lý ngắt dòng (dòng mới) trong Python

Chuyển đổi các loại khác thành chuỗi. print("10 + 20") # Prints: 10 + 20 print("This # is not a comment") # Prints: This # is not a comment print("pow(2,3)") # Prints: pow(2, 3)02

Bạn có thể sử dụng ________ 202 để chuyển đổi các đối tượng thuộc loại khác thành chuỗi (________ 177)

  • Các loại tích hợp -str() — Python 3. 10. 0 tài liệu

print("10 + 20")                  # Prints: 10 + 20
print("This # is not a comment")  # Prints: This # is not a comment
print("pow(2,3)")                 # Prints: pow(2, 3)
02 trả về kết quả của phương thức
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
24 của đối tượng đích. Nếu loại của nó không có phương thức
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
24 được xác định, nó sẽ trả về kết quả của
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
26

Sau đây là một số ví dụ về các loại điển hình

Chuyển đổi số thành chuỗi

Số nguyên

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
27 và số dấu phẩy động
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
28 có thể được chuyển đổi thành chuỗi
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
77 với
print("10 + 20")                  # Prints: 10 + 20
print("This # is not a comment")  # Prints: This # is not a comment
print("pow(2,3)")                 # Prints: pow(2, 3)
02

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
72

nguồn. str_constructor. py

Ví dụ:

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
27 có thể được viết ở dạng thập lục phân,
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
28 ở dạng hàm mũ, v.v., nhưng khi được chuyển đổi thành chuỗi có
print("10 + 20")                  # Prints: 10 + 20
print("This # is not a comment")  # Prints: This # is not a comment
print("pow(2,3)")                 # Prints: pow(2, 3)
02, chúng được chuyển đổi thành chuỗi thập phân tiêu chuẩn

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
73

nguồn. str_constructor. py

Sử dụng hàm

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
34 tích hợp để chuyển đổi nó thành một chuỗi có định dạng mong muốn

  • Định dạng chuỗi và số bằng format() trong Python

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
74

nguồn. str_constructor. py

Nếu bạn muốn chuyển đổi một chuỗi số sang giá trị số, hãy xem bài viết sau

  • Chuyển chuỗi thành số (int, float) trong Python

Chuyển đổi danh sách và từ điển thành chuỗi

Danh sách

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
35 và từ điển
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
36 cũng có thể được chuyển đổi thành chuỗi
He said 'See ya' and closed the door.
They said "We will miss you" as he left.
77 với
print("10 + 20")                  # Prints: 10 + 20
print("This # is not a comment")  # Prints: This # is not a comment
print("pow(2,3)")                 # Prints: pow(2, 3)
02

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
75

nguồn. str_constructor. py

Bạn có thể sử dụng

He said 'See ya' and closed the door.
They said "We will miss you" as he left.
39 của mô-đun pprint của thư viện chuẩn để chuyển đổi nó thành chuỗi được định dạng tốt