For new Python code, start with an f-string: put an f before the quoted string and place values or expressions in braces. Use str.format() when you want to keep a brace-based template separate from its values; recognize % formatting in older code and account for its quirks. The examples below show how to format numbers, align text, pad values, and print literal braces.
Format values with an f-string
An f-string embeds expressions in a string literal. Put an f or F immediately before the opening quote, then wrap each value or expression in braces:
name = "Ada"
score = 9.876
message = f"{name} scored {score:.2f}"
print(message) # Ada scored 9.88
The colon introduces a format specification. In .2f, .2 requests two digits after the decimal point and f selects fixed-point presentation for the number.
F-strings were introduced in Python 3.6. The = debug display was added in Python 3.8, and Python 3.12 relaxed some restrictions on expressions inside f-strings. See the Python tutorial’s input and output section and the built-in types reference for version details.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problems#1 Best Overall
- Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
- Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
- Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
- Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
- 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
Show a value’s representation
Use !r when you want the value’s representation, which is often helpful for inspecting strings because it makes quotes and escape characters visible:
name = "Ada"
print(f"{name!r}") # 'Ada'
F-strings also support !s and !a conversions. The conversion follows the expression and precedes the colon when both are present, as in {value!r:format_spec}.
Rank #2
- 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
- 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
- Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
- 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
- What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
Use debug display
In Python 3.8 and later, add = after an expression to display the expression and its value:
score = 9.876
print(f"{score=}") # score=9.876
print(f"{score=:.2f}") # score=9.88
Control precision, width, and alignment
The format specification mini-language can control presentation after a colon. These examples use f-strings, but the same general format controls are available in str.format() fields. Supported options depend on the value’s type; consult the Python format specification mini-language reference for the full rules.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Rank #3
- Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
- Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
- Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
- Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
- What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
| Goal | Example | Result |
|---|---|---|
| Two digits after the decimal point | f"{9.876:.2f}" |
9.88 |
| Right-align text in a minimum width of 10 | f"{'Ada':>10}" |
Ada |
| Left-align text in a minimum width of 12 | f"{'Ada':<12}" |
Ada |
| Zero-pad a decimal integer to width 4 | f"{42:04d}" |
0042 |
| Group a suitable numeric value with commas | f"{1234567:,}" |
1,234,567 |
Width is a minimum, not a truncation limit: if the formatted value is longer, it is not cut off. Alignment markers include < for left alignment and > for right alignment. A format specification can also include a fill character, sign handling, grouping, precision, and a type code, subject to the type being formatted.
Use `str.format()` with positional or named values
str.format() uses a template string with replacement fields in braces, then receives the values as arguments. Choose positional fields when order is clear, or named fields when labels make the template easier to read:
Rank #4
- Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
- Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
- Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
- Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
- Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
name = "Ada"
score = 9.876
positional = "{} scored {:.2f}".format(name, score)
named = "{player} scored {points:.2f}".format(player=name, points=score)
Named fields can be useful when a template is maintained separately from the values or when an explicit label makes a longer template clearer. A field can refer to an argument, an attribute, or an item by index. The format string syntax reference describes replacement fields and their options.
Print literal braces
In a str.format() template, double braces produce literal braces. Single braces still mark replacement fields:
Best Value
- 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
- Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
- Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
- HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
- What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
name = "Ada"
example = "{{name}} = {name}".format(name=name)
print(example) # {name} = Ada
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Recognize printf-style `%` formatting
The older % operator uses printf-style conversion markers. It remains part of Python, but its rules differ from f-strings and str.format():
name = "Ada"
score = 9.876
message = "%s scored %.2f" % (name, score)
print(message) # Ada scored 9.88
In this example, %s formats the string value and %.2f formats the number with two fractional digits. The built-in types reference documents the operator’s conversion rules and quirks; for new inline strings, f-strings are generally the clearest starting point.
Choose the formatting method that fits
| Method | How values enter the template | Good fit | Version or trade-off |
|---|---|---|---|
| f-string | Expressions are embedded directly in the string literal. | Inline values, calculations, and readable formatting controls. | Available from Python 3.6; debug = from Python 3.8. Expression restrictions changed in Python 3.12. |
str.format() |
Values are supplied as positional or named arguments to a brace-based template. | Templates whose text and values are kept separate, or fields that benefit from names or indexes. | Uses doubled braces for literal braces; see the Python formatting references for field syntax. |
% operator |
Values are supplied to printf-style conversion markers. | Understanding and maintaining existing code that already uses it. | Has conversion rules and quirks documented in the built-in types reference. |
There is no need to switch a working template solely for a presumed speed advantage: the Python references describe the interfaces and their trade-offs, not a general performance benchmark. For new source-code strings, use the style that makes the values and formatting easiest to understand.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

