Để 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 Show
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 PythonChuỗ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
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. 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
Đâ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
Bài viết này mô tả các nội dung sau
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.79Ngoà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épCả hai giá trị đều bằng nhauCho 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 nhauTrong 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 syntax29. Không có vấn đề gì nếu bạn viết File "example.py", line 1 print("This "test" causes problems") ^ SyntaxError: invalid syntax30 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 syntax2 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 syntax30. Không có vấn đề gì nếu bạn viết File "example.py", line 1 print("This "test" causes problems") ^ SyntaxError: invalid syntax29 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 syntax3 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)01Dấ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òngXả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
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épBạ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òngNế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
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)02Bạ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)
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ỗiSố 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
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 đổi danh sách và từ điển thành chuỗiDanh 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 |