Using the str.format() Method in Python

How to Use str.format() in Python

In Python, string formatting is implemented using the str.format() method. This method takes one or more arguments, which are used to replace placeholders in the string with actual values. The placeholders are denoted by curly braces {} in the string and can contain optional format specifiers that control the appearance of the output.

How to Use Python String Formatting?

The str.format() method can be called on any string object and takes one or more arguments that are used to replace placeholders in the string. Here’s a simple example:

name = "John"
age = 25
print("My name is {} and I am {} years old".format(name, age))

Output:

My name is John and I am 25 years old

In this example, we have a string with two placeholders {} that are replaced with the values of the name and age variables using the str.format() method.

Positional Arguments

You can also use positional arguments to specify the order of the values in the output string. Here’s an example:

print("My name is {1} and I am {0} years old".format(age, name))

Output:

My name is John and I am 25 years old

In this example, we have swapped the order of the arguments in the str.format() method so that the value of age is now the first argument and the value of name is the second argument. This changes the order of the values in the output string.

Named Arguments

You can also use named arguments to specify the values to be replaced in the output string. Here’s an example:

print("My name is {name} and I am {age} years old".format(name="John", age=25))

Output:

My name is John and I am 25 years old

In this example, we have used named arguments in the str.format() method to specify the values of name and age directly. This makes the code more readable and easier to understand.

Format Specifiers

You can also use format specifiers to control the appearance of the output values in the string. Here are some examples:

print("The value of pi is approximately {:.2f}".format(3.14159265359))
print("The value of x is {:06d}".format(123))
print("The value of x is {:,}".format(1234567890))
print("The value of x is {:+}".format(123))
print("The value of x is {:#x}".format(123))

Output:

The value of pi is approximately 3.14
The value of x is 000123
The value of x is 1,234,567,890
The value of x is +123
The value of x is 0x7b

In the first example, we have used :.2f to specify that the value of pi should be formatted as a floating-point number with two decimal places. In the second example, we have used :06d to specify that the value of x should be formatted as a zero-padded integer with six digits. In the third example, we have used :, to specify that the value of x should be formatted with commas as the thousands separator. In the fourth example, we have used :+ to specify that the value of x should be formatted with a plus sign for positive numbers. In the fifth example, we have used :#x to specify that the value of x should be formatted as a hexadecimal number with a 0x prefix.

Conclusion

Python string formatting is a powerful feature that allows you to format strings in Python. It is used to embed values or expressions inside string literals, using a special syntax to denote the location and format of the values. This feature is very useful for generating dynamic output or formatting data for display. The str.format() method can be called on any string object and takes one or more arguments that are used to replace placeholders in the string. You can use positional arguments, named arguments, and format specifiers to control the appearance of the output values in the string.