Python format is a method used to format strings in a more readable and user-friendly way. It allows you to insert values into a string in a specific format, making it easier to read and understand. The method uses placeholders, which are enclosed in curly braces {}
, to indicate where values should be inserted into the string.
Basic Usage
To use Python format, you simply need to create a string with placeholders and then pass values to the placeholders using the format method. Here is an example:
name = "John"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
The output of this code would be:
My name is John and I am 25 years old.
In this example, we created a string with two placeholders ({}
) and then passed two values (name and age) to the format method. The method replaced the placeholders with the values, resulting in a formatted string.
Positional Arguments
You can also specify the position of the values you want to insert into the string, using positional arguments. Here is an example:
print("My name is {1} and I am {0} years old.".format(age, name))
The output of this code would be:
My name is John and I am 25 years old.
In this example, we specified the position of the values we wanted to insert into the string using the format method. The method replaced the placeholders with the values in the specified positions, resulting in a formatted string.
Named Arguments
You can also use named arguments to insert values into a string. Here is an example:
print("My name is {name} and I am {age} years old.".format(name="John", age=25))
The output of this code would be:
My name is John and I am 25 years old.
In this example, we used named arguments to insert values into the string. We specified the names of the arguments in the placeholders and then passed the values to the format method using keyword arguments.
Formatting Values
Python format also allows you to format values before inserting them into a string. Here are some examples:
Floating Point Numbers
pi = 3.14159265359
print("The value of pi is approximately {:.2f}.".format(pi))
The output of this code would be:
The value of pi is approximately 3.14.
In this example, we formatted the floating point number pi to two decimal places using the format method.
Integers
num = 12345
print("The value of num is {:,}.".format(num))
The output of this code would be:
The value of num is 12,345.
In this example, we formatted the integer num to include commas using the format method.
Dates
import datetime
today = datetime.datetime.today()
print("Today's date is {:%B %d, %Y}.".format(today))
The output of this code would be:
Today's date is August 01, 2021.
In this example, we formatted the datetime object today to display the month, day, and year in a specific format using the format method.
Padding and Alignment
You can also use the format method to control padding and alignment. For example, you can left-align, right-align, or center-align text within a given width, and pad it with specific characters.
text = "example"
print("Left-aligned: {:<10}".format(text))
print("Right-aligned: {:>10}".format(text))
print("Center-aligned: {:^10}".format(text))
The output of this code would be:
Left-aligned: example
Right-aligned: example
Center-aligned: example
Conclusion
Python format is a powerful method that allows you to create formatted strings that are more readable and easier to work with. It provides several options for inserting values into a string, including positional and named arguments, and allows you to format values before inserting them. By using Python format, you can make your code more user-friendly and easier to maintain.