format method
The string method that returns a copy of a string with each {} replacement field filled in from its arguments.
In more detail
"Hello, {}!".format(name) substitutes name for the braces and converts it to text for you. Fields can be numbered, {0}, or named, {animal}, and a literal brace is written doubled, {{ or }}. The newer f-strings do the same thing more briefly.
Examples
"{} + {} = {}".format(2, 3, 5)Because format can walk attributes, a format string chosen by a caller is a small program, which is the template injection in the course.Related terms
- replacement field — A pair of braces {} in a format string marking where a value will be inserted.
- f-string — A Python string with an f before the opening quote, in which expressions in curly brackets are replaced by their values.
- format specifier — The instructions after a colon inside a replacement field, such as .2f for two decimal places, that control how a value is shown.
- dictionary unpacking — Writing ** before a dictionary in a function call to pass each of its key-value pairs as a keyword argument.
- template injection — An injection attack where a user supplies the template itself, letting them walk into objects and data the template was never meant to show.
- Python — A popular general-purpose programming language known for readable code and a large standard library.
Taught in
- A Sample Program, Explained — Hands-on Python: Complete Video Course & Book
- The String Format Method — Hands-on Python: Complete Video Course & Book
- Multiple Parameters — Hands-on Python: Complete Video Course & Book
- Dictionaries And String Formatting — Hands-on Python: Complete Video Course & Book
- Composing Web Pages In Python — Hands-on Python: Complete Video Course & Book
- Injection In All Its Forms — Application Security & Threat Modeling for Engineers