Tôi chắc chắn thích phương thức print("{0:.<20} {1:.>20} {2:.^20} ".format("Product", "Price", "Sum")) #'Product............. ...............Price ........Sum.........' 7 hơn, vì nó rất linh hoạt và có thể dễ dàng mở rộng đến các lớp tùy chỉnh của bạn bằng cách xác định các biểu diễn print("{0:.<20} {1:.>20} {2:.^20} ".format("Product", "Price", "Sum")) #'Product............. ...............Price ........Sum.........' 8 hoặc print("{0:.<20} {1:.>20} {2:.^20} ".format("Product", "Price", "Sum")) #'Product............. ...............Price ........Sum.........' 9 hoặc class foo(object): def __str__(self): return "...::4::.." def __repr__(self): return "...::12::.." 0. Để giữ cho nó đơn giản, tôi đang sử dụng class foo(object): def __str__(self): return "...::4::.." def __repr__(self): return "...::12::.." 1 trong các ví dụ sau, có thể được thay thế bằng class foo(object): def __str__(self): return "...::4::.." def __repr__(self): return "...::12::.." 2.
#Justify / ALign (left, mid, right) print("{0:<10}".format("Guido")) # 'Guido ' print("{0:>10}".format("Guido")) # ' Guido' print("{0:^10}".format("Guido")) # ' Guido 'Ví dụ đơn giản: căn chỉnh / điền alignment / filling
Chúng ta có thể thêm bên cạnh class foo(object): def __str__(self): return "...::4::.." def __repr__(self): return "...::12::.." 3 chỉ định là class foo(object): def __str__(self): return "...::4::.." def __repr__(self): return "...::12::.." 4, class foo(object): def __str__(self): return "...::4::.." def __repr__(self): return "...::12::.." 5 và class foo(object): def __str__(self): return "...::4::.." def __repr__(self): return "...::12::.." 6 Một ký tự điền để thay thế không gian bằng bất kỳ ký tự nào khác
print("{0:.^10}".format("Guido")) #..Guido...print("{0:.<20} {1:.>20} {2:.^20} ".format("Product", "Price", "Sum")) #'Product............. ...............Price ........Sum.........'Ví dụ đa điểm: Căn chỉnh và điền nhiều đầu vào align and fill many inputs
Ví dụ nâng cao
Nếu bạn có các lớp tùy chỉnh của mình, bạn có thể xác định đó là các biểu diễn print("{0:.<20} {1:.>20} {2:.^20} ".format("Product", "Price", "Sum")) #'Product............. ...............Price ........Sum.........' 9 hoặc class foo(object): def __str__(self): return "...::4::.." def __repr__(self): return "...::12::.." 0 như sau:
class foo(object): def __str__(self): return "...::4::.." def __repr__(self): return "...::12::.."Bây giờ bạn có thể sử dụng class foo(object): def __str__(self): return "...::4::.." def __repr__(self): return "...::12::.." 9 (str) hoặc print "{0!r:<10}".format(x) #'...::12::..' print "{0!s:<10}".format(x) #'...::4::..' 0 (repr) để nói với Python để gọi các phương thức được xác định đó. Nếu không có gì được xác định, Python mặc định là print("{0:.<20} {1:.>20} {2:.^20} ".format("Product", "Price", "Sum")) #'Product............. ...............Price ........Sum.........' 8 cũng có thể được ghi đè. x = foo ()
print "{0!r:<10}".format(x) #'...::12::..' print "{0!s:<10}".format(x) #'...::4::..'Nguồn: Tài liệu tham khảo thiết yếu của Python, David M. Beazley, Phiên bản thứ 4
Giới thiệu
Sớm hay sau, định dạng chuỗi trở thành một điều ác cần thiết cho hầu hết các lập trình viên. Hơn nữa trong quá khứ trước kỷ nguyên GUI máy khách dày, nhưng nhu cầu có một biểu diễn chuỗi cụ thể vẫn là một trường hợp sử dụng đủ phổ biến. Lời giới thiệu đầu tiên của tôi là trở lại trường đại học khi tôi có một giáo sư trường học cũ có tình yêu không tinh khiết để khiến chúng tôi viết các ứng dụng bảng điều khiển Java với các thông số kỹ thuật thần kinh để xuất ra chức năng print "{0!r:<10}".format(x) #'...::12::..' print "{0!s:<10}".format(x) #'...::4::..' 2. Một điều đúng như sau đó và vẫn còn là tài liệu cho định dạng chuỗi (thực tế cho tất cả các ngôn ngữ) để lại rất nhiều điều mong muốn. Tôi hy vọng sẽ giảm bớt nỗi đau này ngày hôm nay bằng cách viết về cách hoàn thành định dạng chuỗi trong Python.
Trong Python có nhiều kỹ thuật để định dạng chuỗi, chính xác là bốn kỹ thuật. Điều thú vị là loại này đi ngược lại với tư duy của Python rằng nói chung nên có một cách rõ ràng tốt nhất để hoàn thành một nhiệm vụ. Tuy nhiên, nếu bạn đã dành bất kỳ khoảng thời gian hợp lý nào với ngôn ngữ, bạn có thể đã thấy những kỹ thuật khác nhau này và tự hỏi, "Điều gì là thỏa thuận với tất cả những điều này?".
Chạy qua các kỹ thuật định dạng bốn chuỗi
Hãy bắt đầu với phương pháp thường thấy nhất sử dụng toán tử print "{0!r:<10}".format(x) #'...::12::..' print "{0!s:<10}".format(x) #'...::4::..' 3. Tôi nói phổ biến nhất đơn giản vì nó đã tồn tại lâu nhất và bạn thấy nó ở khắp mọi nơi (sách, bài viết trên blog, Overflow, v.v.). Để sử dụng phương pháp này, bạn chỉ định một trình giữ chỗ trong một chuỗi bằng cách sử dụng print "{0!r:<10}".format(x) #'...::12::..' print "{0!s:<10}".format(x) #'...::4::..' 4 cho các chuỗi và print "{0!r:<10}".format(x) #'...::12::..' print "{0!s:<10}".format(x) #'...::4::..' 5 cho các số.
>>> "Hello reader, welcome to the %s form of string formatting." % 'modulus' 'Hello reader, welcome to the modulus form of string formatting.' >>> >>> "Formatting multiple (%d, %d, %d, ...) values requires a %s." % (1, 2, 3, 'tuple') 'Formatting multiple (1, 2, 3, ...) values requires a tuple.' >>> >>> print("""If you prefer named placeholders for values %(one)d, %(two)d, ... %(three)d ... you can use a dict""" % {'one':1, 'two':2, 'three':3}) If you prefer named placeholders for values 1, 2, 3 ... you can use a dictKỹ thuật tiếp theo để bao gồm là phương pháp print "{0!r:<10}".format(x) #'...::12::..' print "{0!s:<10}".format(x) #'...::4::..' 6. Đây được dự định là sự thay thế cho kiểu print "{0!r:<10}".format(x) #'...::12::..' print "{0!s:<10}".format(x) #'...::4::..' 3 được hiển thị trước đó. Kỹ thuật này sử dụng dấu ngoặc xoăn print "{0!r:<10}".format(x) #'...::12::..' print "{0!s:<10}".format(x) #'...::4::..' 8 Chỉ định để chỉ định vị trí và cách định dạng giá trị thành một chuỗi.
>>> "Hello reader, welcome to the {} form of string formatting".format('str.format(...)') 'Hello reader, welcome to the str.format(...) form of string formatting' >>> >>> print("""Formatting multiple ({0}, {1}, {2}, ...) values requires ... that you use multiple {3} brackets and optionally specify ordering ... values.""".format(1,2,3,'{}')) Formatting multiple (1, 2, 3, ...) values requires that you use multiple {} brackets and optionally specify ordering values. >>> >>> print("""The {language} str.format() method also allows you to use ... named parameters which help keep code {adjective} ... """.format(language='Python', adjective='concise')) The Python str.format() method also allows you to use named parameters which help keep code conciseSau đó, có kỹ thuật mẫu chuỗi, là một lớp trong mô -đun chuỗi. Phương pháp định dạng chuỗi này là một chút dài dòng và không hỗ trợ các nhà xác định loại (s, d, f, v.v.), không giống như hai loại trước đó. Với kỹ thuật này, bạn chỉ định trình giữ chỗ trong một chuỗi bằng cách tiền tố giá trị mong muốn với print "{0!r:<10}".format(x) #'...::12::..' print "{0!s:<10}".format(x) #'...::4::..' 9 trong hàm tạo của lớp >>> "Hello reader, welcome to the %s form of string formatting." % 'modulus' 'Hello reader, welcome to the modulus form of string formatting.' >>> >>> "Formatting multiple (%d, %d, %d, ...) values requires a %s." % (1, 2, 3, 'tuple') 'Formatting multiple (1, 2, 3, ...) values requires a tuple.' >>> >>> print("""If you prefer named placeholders for values %(one)d, %(two)d, ... %(three)d ... you can use a dict""" % {'one':1, 'two':2, 'three':3}) If you prefer named placeholders for values 1, 2, 3 ... you can use a dict 0, sau đó bạn gọi phương thức >>> "Hello reader, welcome to the %s form of string formatting." % 'modulus' 'Hello reader, welcome to the modulus form of string formatting.' >>> >>> "Formatting multiple (%d, %d, %d, ...) values requires a %s." % (1, 2, 3, 'tuple') 'Formatting multiple (1, 2, 3, ...) values requires a tuple.' >>> >>> print("""If you prefer named placeholders for values %(one)d, %(two)d, ... %(three)d ... you can use a dict""" % {'one':1, 'two':2, 'three':3}) If you prefer named placeholders for values 1, 2, 3 ... you can use a dict 1 trên đối tượng khởi tạo với tham số được đặt tên. Phương pháp này ít phổ biến hơn do sức mạnh và tính linh hoạt của nó.
>>> from string import Template >>> tmpl = Template("Hello my name is $name") >>> tmpl.substitute(name='Adam') 'Hello my name is Adam'Kỹ thuật cuối cùng và triển khai gần đây nhất chỉ có trong Python 3.6, được gọi là nội suy chuỗi. Điều này có điểm tương đồng với chuỗi mẫu ES6 JavaScript. Các phép nội suy chuỗi yêu cầu chuỗi theo nghĩa đen được có tiền tố với >>> "Hello reader, welcome to the %s form of string formatting." % 'modulus' 'Hello reader, welcome to the modulus form of string formatting.' >>> >>> "Formatting multiple (%d, %d, %d, ...) values requires a %s." % (1, 2, 3, 'tuple') 'Formatting multiple (1, 2, 3, ...) values requires a tuple.' >>> >>> print("""If you prefer named placeholders for values %(one)d, %(two)d, ... %(three)d ... you can use a dict""" % {'one':1, 'two':2, 'three':3}) If you prefer named placeholders for values 1, 2, 3 ... you can use a dict 2 và cho phép cả hai biểu thức và biến được chỉ định trực tiếp trong chuỗi miễn là chúng được bao quanh bởi các dấu ngoặc print "{0!r:<10}".format(x) #'...::12::..' print "{0!s:<10}".format(x) #'...::4::..' 8.
>>> method="String Interpolation" >>> f"Hello reader, I am the {method} of formatting" 'Hello reader, I am the String Interpolation of formatting' >>> >>> f"With this method you can have expressions like {{1 + 1}} = {1 + 1}" 'With this method you can have expressions like {1 + 1} = 2'Đào sâu hơn vào định dạng chuỗi
Trong các phần sau, tôi sẽ giới hạn cuộc thảo luận chỉ trong phương pháp >>> "Hello reader, welcome to the %s form of string formatting." % 'modulus' 'Hello reader, welcome to the modulus form of string formatting.' >>> >>> "Formatting multiple (%d, %d, %d, ...) values requires a %s." % (1, 2, 3, 'tuple') 'Formatting multiple (1, 2, 3, ...) values requires a tuple.' >>> >>> print("""If you prefer named placeholders for values %(one)d, %(two)d, ... %(three)d ... you can use a dict""" % {'one':1, 'two':2, 'three':3}) If you prefer named placeholders for values 1, 2, 3 ... you can use a dict 4 và kỹ thuật nội suy >>> "Hello reader, welcome to the %s form of string formatting." % 'modulus' 'Hello reader, welcome to the modulus form of string formatting.' >>> >>> "Formatting multiple (%d, %d, %d, ...) values requires a %s." % (1, 2, 3, 'tuple') 'Formatting multiple (1, 2, 3, ...) values requires a tuple.' >>> >>> print("""If you prefer named placeholders for values %(one)d, %(two)d, ... %(three)d ... you can use a dict""" % {'one':1, 'two':2, 'three':3}) If you prefer named placeholders for values 1, 2, 3 ... you can use a dict 2 vì chúng là các phương pháp ưa thích để định dạng chuỗi. Các chủ đề tôi muốn đào sâu hơn bao gồm:
- Căn chỉnh văn bản
- Định dạng số
- Nhập chuyển đổi
Cả >>> "Hello reader, welcome to the %s form of string formatting." % 'modulus' 'Hello reader, welcome to the modulus form of string formatting.' >>> >>> "Formatting multiple (%d, %d, %d, ...) values requires a %s." % (1, 2, 3, 'tuple') 'Formatting multiple (1, 2, 3, ...) values requires a tuple.' >>> >>> print("""If you prefer named placeholders for values %(one)d, %(two)d, ... %(three)d ... you can use a dict""" % {'one':1, 'two':2, 'three':3}) If you prefer named placeholders for values 1, 2, 3 ... you can use a dict 4 và các kỹ thuật nội suy đều có cùng cú pháp để xác định định dạng giữa các dấu ngoặc print "{0!r:<10}".format(x) #'...::12::..' print "{0!s:<10}".format(x) #'...::4::..' 8 sử dụng >>> "Hello reader, welcome to the %s form of string formatting." % 'modulus' 'Hello reader, welcome to the modulus form of string formatting.' >>> >>> "Formatting multiple (%d, %d, %d, ...) values requires a %s." % (1, 2, 3, 'tuple') 'Formatting multiple (1, 2, 3, ...) values requires a tuple.' >>> >>> print("""If you prefer named placeholders for values %(one)d, %(two)d, ... %(three)d ... you can use a dict""" % {'one':1, 'two':2, 'three':3}) If you prefer named placeholders for values 1, 2, 3 ... you can use a dict 8 để phân tách các định danh có tên hoặc thứ tự ở bên trái và định dạng thông số kỹ thuật ở bên phải.
Căn chỉnh văn bản
Bạn có thể căn chỉnh các giá trị trong một độ dài văn bản được chỉ định bằng cách sử dụng các ký hiệu class foo(object): def __str__(self): return "...::4::.." def __repr__(self): return "...::12::.." 5, class foo(object): def __str__(self): return "...::4::.." def __repr__(self): return "...::12::.." 6 hoặc class foo(object): def __str__(self): return "...::4::.." def __repr__(self): return "...::12::.." 4 để chỉ định căn chỉnh bên trái, căn chỉnh bên phải hoặc tâm tương ứng. Sau đó, bạn làm theo những biểu tượng đó với chiều rộng nhân vật mà bạn mong muốn.
Python> 2.6:
>>> left_aligned = "Left Align" >>> center = "Centered" >>> right_aligned = "Right Align" >>> "{left_aligned:<15}{center:^10}{right_aligned:>15}".format( ... left_aligned=left_aligned, ... center=center, ... right_aligned=right_aligned) 'Left Align Centered Right Align'Bạn cũng có thể chỉ định các vị trí thứ tự thay vì từ khóa.
print("{0:.^10}".format("Guido")) #..Guido... 0Hoặc, bạn có thể bỏ qua chúng nếu thứ tự của các tham số thành >>> "Hello reader, welcome to the {} form of string formatting".format('str.format(...)') 'Hello reader, welcome to the str.format(...) form of string formatting' >>> >>> print("""Formatting multiple ({0}, {1}, {2}, ...) values requires ... that you use multiple {3} brackets and optionally specify ordering ... values.""".format(1,2,3,'{}')) Formatting multiple (1, 2, 3, ...) values requires that you use multiple {} brackets and optionally specify ordering values. >>> >>> print("""The {language} str.format() method also allows you to use ... named parameters which help keep code {adjective} ... """.format(language='Python', adjective='concise')) The Python str.format() method also allows you to use named parameters which help keep code concise 2 là cùng thứ tự của print "{0!r:<10}".format(x) #'...::12::..' print "{0!s:<10}".format(x) #'...::4::..' 8.
print("{0:.^10}".format("Guido")) #..Guido... 1Python 3.6:
print("{0:.^10}".format("Guido")) #..Guido... 2Trong các ví dụ trước, tôi hoàn toàn lấp đầy không gian đệm còn lại với các khoảng trống là hành vi mặc định. Tuy nhiên, nếu đây không phải là những gì bạn mong muốn, bạn có thể lấp đầy chúng bằng một cái gì đó khác nhau bằng cách chỉ định một nhân vật ngay sau đại tràng.
Python> 2.6:
print("{0:.^10}".format("Guido")) #..Guido... 3Python 3.6:
print("{0:.^10}".format("Guido")) #..Guido... 4Số
Định dạng số điểm nổi, những người chứa các vị trí thập phân, là một cinch trong Python. Tất cả những gì bạn cần làm là theo dấu hai chấm với >>> "Hello reader, welcome to the {} form of string formatting".format('str.format(...)') 'Hello reader, welcome to the str.format(...) form of string formatting' >>> >>> print("""Formatting multiple ({0}, {1}, {2}, ...) values requires ... that you use multiple {3} brackets and optionally specify ordering ... values.""".format(1,2,3,'{}')) Formatting multiple (1, 2, 3, ...) values requires that you use multiple {} brackets and optionally specify ordering values. >>> >>> print("""The {language} str.format() method also allows you to use ... named parameters which help keep code {adjective} ... """.format(language='Python', adjective='concise')) The Python str.format() method also allows you to use named parameters which help keep code concise 4.
Python> 2.6:
print("{0:.^10}".format("Guido")) #..Guido... 5Số
Python 3.6:
print("{0:.^10}".format("Guido")) #..Guido... 6Định dạng số điểm nổi, những người chứa các vị trí thập phân, là một cinch trong Python. Tất cả những gì bạn cần làm là theo dấu hai chấm với >>> "Hello reader, welcome to the {} form of string formatting".format('str.format(...)') 'Hello reader, welcome to the str.format(...) form of string formatting' >>> >>> print("""Formatting multiple ({0}, {1}, {2}, ...) values requires ... that you use multiple {3} brackets and optionally specify ordering ... values.""".format(1,2,3,'{}')) Formatting multiple (1, 2, 3, ...) values requires that you use multiple {} brackets and optionally specify ordering values. >>> >>> print("""The {language} str.format() method also allows you to use ... named parameters which help keep code {adjective} ... """.format(language='Python', adjective='concise')) The Python str.format() method also allows you to use named parameters which help keep code concise 4.
Python> 2.6:
print("{0:.^10}".format("Guido")) #..Guido... 7Python 3.6:
print("{0:.^10}".format("Guido")) #..Guido... 8Số
Python> 2.6:
print("{0:.^10}".format("Guido")) #..Guido... 9Python 3.6:
print("{0:.<20} {1:.>20} {2:.^20} ".format("Product", "Price", "Sum")) #'Product............. ...............Price ........Sum.........' 0Số
Python> 2.6:
print("{0:.<20} {1:.>20} {2:.^20} ".format("Product", "Price", "Sum")) #'Product............. ...............Price ........Sum.........' 1Python 3.6:
print("{0:.<20} {1:.>20} {2:.^20} ".format("Product", "Price", "Sum")) #'Product............. ...............Price ........Sum.........' 2Số
Python> 2.6:
print("{0:.<20} {1:.>20} {2:.^20} ".format("Product", "Price", "Sum")) #'Product............. ...............Price ........Sum.........' 3Python 3.6:
print("{0:.<20} {1:.>20} {2:.^20} ".format("Product", "Price", "Sum")) #'Product............. ...............Price ........Sum.........' 4Số
Định dạng số điểm nổi, những người chứa các vị trí thập phân, là một cinch trong Python. Tất cả những gì bạn cần làm là theo dấu hai chấm với >>> "Hello reader, welcome to the {} form of string formatting".format('str.format(...)') 'Hello reader, welcome to the str.format(...) form of string formatting' >>> >>> print("""Formatting multiple ({0}, {1}, {2}, ...) values requires ... that you use multiple {3} brackets and optionally specify ordering ... values.""".format(1,2,3,'{}')) Formatting multiple (1, 2, 3, ...) values requires that you use multiple {} brackets and optionally specify ordering values. >>> >>> print("""The {language} str.format() method also allows you to use ... named parameters which help keep code {adjective} ... """.format(language='Python', adjective='concise')) The Python str.format() method also allows you to use named parameters which help keep code concise 4.
Để chỉ định một độ chính xác khác (số lượng thập phân) chỉ đơn giản là đi trước >>> "Hello reader, welcome to the {} form of string formatting".format('str.format(...)') 'Hello reader, welcome to the str.format(...) form of string formatting' >>> >>> print("""Formatting multiple ({0}, {1}, {2}, ...) values requires ... that you use multiple {3} brackets and optionally specify ordering ... values.""".format(1,2,3,'{}')) Formatting multiple (1, 2, 3, ...) values requires that you use multiple {} brackets and optionally specify ordering values. >>> >>> print("""The {language} str.format() method also allows you to use ... named parameters which help keep code {adjective} ... """.format(language='Python', adjective='concise')) The Python str.format() method also allows you to use named parameters which help keep code concise 4 theo số lượng vị trí thập phân mong muốn như vậy. | Một trường hợp sử dụng định dạng khác cho số điểm nổi là phần trăm xác định. Điều này hoạt động bằng cách chuyển đổi những gì dự kiến là tỷ lệ hoặc tỷ lệ (0-1) thành giá trị trong số 100 và xử lý phần thập phân còn sót lại tương tự như trình xác định >>> "Hello reader, welcome to the {} form of string formatting".format('str.format(...)') 'Hello reader, welcome to the str.format(...) form of string formatting' >>> >>> print("""Formatting multiple ({0}, {1}, {2}, ...) values requires ... that you use multiple {3} brackets and optionally specify ordering ... values.""".format(1,2,3,'{}')) Formatting multiple (1, 2, 3, ...) values requires that you use multiple {} brackets and optionally specify ordering values. >>> >>> print("""The {language} str.format() method also allows you to use ... named parameters which help keep code {adjective} ... """.format(language='Python', adjective='concise')) The Python str.format() method also allows you to use named parameters which help keep code concise 4 với độ chính xác mặc định là sáu. |
OK, để chăm sóc hầu hết các trường hợp sử dụng liên quan đến số điểm nổi, nhưng còn số lượng lớn thì sao? Python cũng có sự hỗ trợ cho việc định dạng chúng bằng cách đưa vào dấu phẩy để tăng khả năng đọc của số lượng lớn. Để tận dụng điều này, chỉ cần đặt một >>> "Hello reader, welcome to the {} form of string formatting".format('str.format(...)') 'Hello reader, welcome to the str.format(...) form of string formatting' >>> >>> print("""Formatting multiple ({0}, {1}, {2}, ...) values requires ... that you use multiple {3} brackets and optionally specify ordering ... values.""".format(1,2,3,'{}')) Formatting multiple (1, 2, 3, ...) values requires that you use multiple {} brackets and optionally specify ordering values. >>> >>> print("""The {language} str.format() method also allows you to use ... named parameters which help keep code {adjective} ... """.format(language='Python', adjective='concise')) The Python str.format() method also allows you to use named parameters which help keep code concise 7 sau đại tràng. | Nhập chuyển đổi |
Chuyển đổi loại là một chút của một trường hợp sử dụng ít phổ biến hơn, nhưng thỉnh thoảng chúng lại xuất hiện. Các chuyển đổi loại chính là sau đây cho các số: | Chuyển đổi |
Sự mô tả | b |
Nhị phân
Python> 2.6:
print("{0:.<20} {1:.>20} {2:.^20} ".format("Product", "Price", "Sum")) #'Product............. ...............Price ........Sum.........' 5Python 3.6:
print("{0:.<20} {1:.>20} {2:.^20} ".format("Product", "Price", "Sum")) #'Product............. ...............Price ........Sum.........' 6Số
Định dạng số điểm nổi, những người chứa các vị trí thập phân, là một cinch trong Python. Tất cả những gì bạn cần làm là theo dấu hai chấm với >>> "Hello reader, welcome to the {} form of string formatting".format('str.format(...)') 'Hello reader, welcome to the str.format(...) form of string formatting' >>> >>> print("""Formatting multiple ({0}, {1}, {2}, ...) values requires ... that you use multiple {3} brackets and optionally specify ordering ... values.""".format(1,2,3,'{}')) Formatting multiple (1, 2, 3, ...) values requires that you use multiple {} brackets and optionally specify ordering values. >>> >>> print("""The {language} str.format() method also allows you to use ... named parameters which help keep code {adjective} ... """.format(language='Python', adjective='concise')) The Python str.format() method also allows you to use named parameters which help keep code concise 4.