Python chấp nhận ba loại dấu ngoặc để biểu diễn chuỗi (string literals), miễn là cùng loại đóng mở:
- Dấu nháy đơn:
'word' - Dấu nháy kép:
"This is a sentence." - Dấu nháy ba (triple quotes):
'''...'''hoặc"""..."""- dùng cho chuỗi đa dòng
Ví dụ hợp lệ
word = 'word' sentence = "This is a sentence." paragraph = """This is a paragraph. It is made up of multiple lines and sentences.""" # Nhúng dấu nháy trong chuỗi quote = "He said, 'Hello!'" quote2 = 'He said, "Hello!"' quote3 = """He said, "Hello!" and 'Hi!'"""
Raw String (Chuỗi thô)
Tiền tố r hoặc R tắt xử lý escape sequence:
normal = "C:\new\path" # \n = newline, \p = p raw = r"C:\new\path" # Giữ nguyên \n \p print(normal) print(raw)
F-string (Python 3.6+)
name = "Python"
version = 3.11
print(f"Học {name} {version}") # Học Python 3.11
print(f"{name:{'>10}}") # Căn phải độ rộng 10