In Python f-strings, placeholders are enclosed in curly braces {} and can contain various format specifiers and expressions to format values. Here are some common placeholders and format specifiers you can use within f-strings:
-
Basic Value Insertion:
- {variable}: Insert the value of the variable.
- {expression}: Insert the result of the expression.
-
String Formatting:
- {variable:s}: Insert the value of the variable as a string.
- {variable:<width}: Left-align the string within a field of the specified width.
- {variable:^width}: Center-align the string within a field of the specified width.
- {variable:>width}: Right-align the string within a field of the specified width.
- {variable:.precisionf}: Format a floating-point number with the specified precision.
- {variable:.precisions}: Format a string with the specified length (right-padded with spaces if needed).
-
Integer Formatting:
- {variable:d}: Insert the value of the variable as an integer.
- {variable:widthd}: Format an integer with a minimum field width.
- {variable:0widthd}: Format an integer with leading zeros.
-
Floating-Point Formatting:
- {variable:f}: Insert the value of the variable as a floating-point number.
- {variable:.precisionf}: Format a floating-point number with the specified precision.
- {variable:e}: Format a floating-point number in scientific notation.
- {variable:.precisione}: Format a floating-point number in scientific notation with the specified precision.
-
Hexadecimal and Binary Formatting:
- {variable:x}: Format an integer as a hexadecimal string (lowercase).
- {variable:X}: Format an integer as a hexadecimal string (uppercase).
- {variable:b}: Format an integer as a binary string.
-
Date and Time Formatting:
- {variable:%format}: Format a datetime object using Python's strftime format.
For example, here's how you can use some of these placeholders and format specifiers in f-strings:
name = "Alice"
age = 30
pi = 3.14159
print(f"Hello, {name}! You are {age} years old.")
print(f"Value of pi: {pi:.2f}")
print(f"Binary representation of 10: {10:b}")