Formatting Output in Python
Formatting Output in Python
In Python, formatting output allows you to present data in a readable and structured way. There are several methods for formatting output in Python, which can be useful for making your program’s output more user-friendly. Let’s explore the most common methods.
1. Using f-strings (Formatted String Literals)
Introduced in Python 3.6, f-strings are the most modern and efficient way to format strings. You can embed expressions inside string literals using curly braces {}
and prefix the string with f
.
Syntax:
Example:
Output:
Advantages of f-strings:
More readable and concise.
Allows expressions inside the curly braces, such as calculations or function calls.
Example with expressions:
Output:
2. Using str.format()
Method
str.format()
MethodThe str.format()
method allows you to format strings by inserting placeholders in the string and passing values to these placeholders.
Syntax:
Example:
Output:
Using Indexes and Named Placeholders:
You can also specify the position of the arguments using indexes or name them.
Example with indexes:
Output:
Example with named placeholders:
Output:
3. Using %
Formatting (Old Style)
%
Formatting (Old Style)This method is an older way to format strings in Python. It is similar to the printf()
function in C. Although it is still widely used, it is generally recommended to use f-strings or str.format()
for better readability.
Syntax:
Example:
Output:
Common Format Specifiers:
%s
for strings.%d
for integers.%f
for floating-point numbers.
Example with floating-point number:
Output:
4. Controlling Width and Alignment
You can control the width and alignment of the output using formatting techniques.
1. Right-aligning text
Use >
to right-align text in a field of a given width.
Example:
Output:
2. Left-aligning text
Use <
to left-align text in a field of a given width.
Example:
Output:
3. Center-aligning text
Use ^
to center-align text in a field of a given width.
Example:
Output:
5. Formatting Numbers
You can format numbers in Python to control decimal places, add commas for thousands, or represent them in scientific notation.
1. Controlling Decimal Places
Use :.nf
to control the number of decimal places, where n
is the number of decimal places you want.
Example:
Output:
2. Adding Commas for Thousands
Use :,
to format numbers with commas as thousands separators.
Example:
Output:
6. Combining Multiple Formatting Techniques
You can combine different formatting techniques in a single string.
Example:
Output:
Conclusion
Python offers several ways to format output, from the modern f-strings to the older %
formatting. The most commonly recommended and efficient approach is using f-strings, as they are easier to read and allow for complex expressions. However, depending on your needs, you can also use str.format()
or %
formatting.
Here’s a quick summary:
f-strings (recommended):
f"Hello, {name}"
str.format()
:"Hello, {}".format(name)
Old style (
%
):"Hello, %s" % name
Happy Coding at codeswithpankaj.com! 🚀
Last updated