In Python, format specifiers are used in string formatting operations to control how the values of variables are displayed within the resulting string.
They allow you to define the width, precision, alignment, and other formatting options for the values being inserted into the string. Format specifiers are denoted by curly braces {}
within the string and are followed by a colon :
and the format specification.
Here’s the basic syntax for using format specifiers:
formatted_string = "Some text with {} and {} formatted values".format(value1, value2)
Starting from Python 3.6, f-strings (formatted string literals) were introduced, which provide a more concise and expressive way to format strings. You can use curly braces {}
with an equal sign =
and place an expression or variable inside the braces directly.
formatted_string = f"Some text with {value1} and {value2} formatted values"
Here’s a tabular format with some commonly used format specifiers in Python string formatting:
Format Specifier | Description | Example Input | Example Output |
---|---|---|---|
%s or {} | Placeholder for strings. | "Hello" | "Hello" |
%10s or {:<10} | String with a minimum width of 10 characters (left-aligned). | "Hello" | "Hello " |
%-10s or {:>10} | String with a minimum width of 10 characters (right-aligned). | "Hello" | " Hello" |
%10.5s or {:^10.5} | String with a minimum width of 10 characters and a maximum width of 5 characters (center-aligned). | "Hello" | " Hello " |
%d or {} | Placeholder for integers (decimal). | 42 | "42" |
%5d or {:<5} | Integer with a minimum width of 5 characters (left-aligned). | 42 | "42 " |
%-5d or {:>5} | Integer with a minimum width of 5 characters (right-aligned). | 42 | " 42" |
%05d or {:^5} | Integer with a minimum width of 5 characters (zero-padded). | 42 | "00042" |
%f or {} | Placeholder for floating-point numbers. | 3.14159 | "3.141590" |
%10.2f or {:<10.2f} | Floating-point number with a minimum width of 10 characters and 2 decimal places (left-aligned). | 3.14159 | "3.14 " |
%-10.2f or {:>10.2f} | Floating-point number with a minimum width of 10 characters and 2 decimal places (right-aligned). | 3.14159 | " 3.14" |
%10.2e or {:^10.2e} | Floating-point number in scientific notation with a minimum width of 10 characters and 2 decimal places (center-aligned). | 1000000.0 | "1.00e+06" |
%o or {} | Placeholder for octal numbers. | 16 | "20" |
%x or {} | Placeholder for hexadecimal (lowercase) numbers. | 255 | "ff" |
%X or {} | Placeholder for hexadecimal (uppercase) numbers. | 255 | "FF" |
%b or {} | Placeholder for binary numbers. | 10 | "1010" |
%% or {}% | Literal percent sign. | – | "%" |
Note: the example outputs depend on the width specified and the values given.
Python format specifiers examples
Let’s go through some examples of using format specifiers in Python for string formatting:
Basic String Formatting:
name = "John"
age = 25
formatted_string = "My name is {} and I am {} years old.".format(name, age)
print(formatted_string)
# Output: "My name is John and I am 25 years old."
Formatting Floating-Point Numbers:
pi = 3.14159265359
formatted_pi = "The value of pi is {:.2f}".format(pi)
print(formatted_pi)
# Output: "The value of pi is 3.14"
Specifying Width and Alignment:
item = "Widget"
price = 42.99
formatted_item = "{:<10} = ${:>6.2f}".format(item, price)
print(formatted_item)
# Output: "Widget = $ 42.99"
Formatting Integers and Binary Representation:
num = 42
formatted_int = "Decimal: {:d}, Hexadecimal: {:x}, Binary: {:b}".format(num, num, num)
print(formatted_int)
# Output: "Decimal: 42, Hexadecimal: 2a, Binary: 101010"
Formatting with f-strings (Python 3.6+):
name = "Alice"
age = 30
formatted_string = f"My name is {name} and I am {age} years old."
print(formatted_string)
# Output: "My name is Alice and I am 30 years old."
Formatting Percentage:
success_rate = 0.876
formatted_percentage = "Success Rate: {:.2%}".format(success_rate)
print(formatted_percentage)
Output:
Below are some commonly used format specifiers:
- Placeholder for Strings:
%s
or{}
: String%10s
or{:<10}
: String with a minimum width of 10 characters (left-aligned)%-10s
or{:>10}
: String with a minimum width of 10 characters (right-aligned)%10.5s
or{:^10.5}
: String with a minimum width of 10 characters and a maximum width of 5 characters (center-aligned)
- Placeholder for Integers:
%d
or{}
: Integer (decimal)%5d
or{:<5}
: Integer with a minimum width of 5 characters (left-aligned)%-5d
or{:>5}
: Integer with a minimum width of 5 characters (right-aligned)%05d
or{:^5}
: Integer with a minimum width of 5 characters (zero-padded)
- Placeholder for Floating-Point Numbers:
%f
or{}
: Floating-point number%10.2f
or{:<10.2f}
: Floating-point number with a minimum width of 10 characters and 2 decimal places (left-aligned)%-10.2f
or{:>10.2f}
: Floating-point number with a minimum width of 10 characters and 2 decimal places (right-aligned)%10.2e
or{:^10.2e}
: Floating-point number in scientific notation with a minimum width of 10 characters and 2 decimal places (center-aligned)
- Placeholder for Octal, Hexadecimal, and Binary Numbers:
%o
or{}
: Octal%x
or{}
: Hexadecimal (lowercase)%X
or{}
: Hexadecimal (uppercase)%b
or{}
: Binary
- Placeholder for Percentages:
%%
or{}%
: Literal percent sign
These are just a few examples, and there are more format specifiers and formatting options available in Python. For more details, you can refer to the official Python documentation on string formatting: https://docs.python.org/3/library/string.html#format-specification-mini-language
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.