The format()
function in Python is a powerful tool for constructing strings that are both readable and customizable. It allows you to insert variables and values into strings, control their formatting, and enhance the overall presentation of your output.
Understanding the Basics
At its core, the format()
function offers a flexible way to embed variables and values within a string template. The template itself can contain placeholders marked by curly braces {}
. These placeholders act as containers where the values will be inserted during the formatting process.
Syntax and Parameters
The general syntax for the format()
function is as follows:
formatted_string = "String template with {} placeholders".format(value1, value2, ...)
Let's break down the components:
formatted_string
: This variable will hold the final formatted string after theformat()
function has been applied."String template with {} placeholders"
: This is the string template containing placeholders that represent where the values will be inserted.format(value1, value2, ...)
: This is the part where you specify the values to be inserted into the placeholders. You can provide multiple values separated by commas.
Simple Formatting Examples
Let's explore some fundamental examples to illustrate how the format()
function operates.
Example 1: Basic Insertion
name = "Alice"
age = 30
formatted_string = "My name is {} and I am {} years old.".format(name, age)
print(formatted_string)
Output:
My name is Alice and I am 30 years old.
In this example, the name
and age
variables are inserted into the respective placeholders within the string template.
Example 2: Positional Formatting
number1 = 10
number2 = 20
formatted_string = "The sum of {} and {} is {}.".format(number1, number2, number1 + number2)
print(formatted_string)
Output:
The sum of 10 and 20 is 30.
Here, the values are inserted based on their positions in the format()
function's arguments.
Example 3: Keyword Formatting
city = "London"
country = "United Kingdom"
formatted_string = "I live in {city}, which is located in {country}.".format(city=city, country=country)
print(formatted_string)
Output:
I live in London, which is located in United Kingdom.
In this case, we explicitly assign the values to the placeholders using keyword arguments. This makes the code more readable and easier to maintain.
Advanced Formatting Options
Beyond basic insertion, the format()
function allows you to control the presentation of your values using various formatting codes.
Example 4: Number Formatting
number = 1234.5678
formatted_string = "The number is {:.2f}".format(number)
print(formatted_string)
Output:
The number is 1234.57
Here, :.2f
specifies that we want to format the number as a floating-point value with two decimal places.
Example 5: String Alignment
name = "Bob"
greeting = "Hello"
formatted_string = "{:<10} {:<10}".format(greeting, name)
print(formatted_string)
Output:
Hello Bob
In this example, :<10
indicates left alignment within a field of 10 characters.
Example 6: Multiple Formatting Codes
price = 19.99
formatted_string = "The price is ${:.2f}".format(price)
print(formatted_string)
Output:
The price is $19.99
You can combine formatting codes like $
and :.2f
to achieve desired formatting.
Conclusion
The Python format()
function provides a flexible and powerful way to format strings in your code. Whether you need to insert variables, align text, or control the presentation of numbers, the format()
function offers a range of options to meet your needs. Remember to explore the full potential of formatting codes to achieve your desired string representation.