Python String Format Method
In python, the string format() method is useful to format complex strings and numbers. The format strings will contain the curly braces { } and the format() method will use those curly braces { } as placeholders to replace with the content of the parameters.
Following is the pictorial representation of string format() method functionality in python.

If you observe the above diagram, the format string (s) contains placeholders ({ }), and the python format method inserted the required text in the place of placeholders and returned the formatted string.
String Format Method Syntax
Following is the syntax of defining a string format method in python.
The python format() method will accept unlimited arguments and replace the curly brace { } placeholders with the content of the respective arguments.
String Format Method Example
Following is the example of formatting the strings using the format() method in python.
print(str1)
name = "Suresh"
age = 33
str2 = "My name is {}, and I am {} years old"
print(str2.format(name, age))
If you observe the above example, we created strings with a curly brace { } placeholders and we are replacing the placeholders with required argument values using format() method.
When you execute the above python program, you will get the result as shown below.
My name is Suresh, and I am 33 years old
If you observe the above example, we created strings with placeholders without specifying any order, and we are not sure whether the arguments are placed in the correct order or not.
String Format with Positional/Keyword Arguments
To make sure the arguments are placed in the correct order, you can define the positional or keyword arguments in the placeholders like as shown below.
str1 = "{1} to {0}".format("Tutlane.com", "Welcome")
print(str1)
# Keyword arguments
name = "Suresh"
age = 33
str2 = "My name is {x}, and I am {y} years old"
print(str2.format(y = age, x = name))
If you observe the above example, we defined the placeholders with positional and keyword arguments to specify the order.
When you execute the above python program, you will get the result as shown below.
My name is Suresh, and I am 33 years old
String Format Placeholder Types
In python, you can format the placeholder's text by using different formatting types like <, >, ^, etc., based on your requirements.
The following table lists different type of format types you can use inside of placeholders to format the text.
Following is the example of using formatting the strings using different format types in python.
msg1 = "Hi {:<10}, welcome"
print(msg1.format("guest"))
# ">" right-align the value
msg2 = "Hi {:>10}, welcome"
print(msg2.format("guest"))
# "^" center-align the value
msg3 = "Hi {:^10}, welcome"
print(msg3.format("guest"))
# "=" place +/- sign at the left most position
msg4 = "The amount is {:=8}, pay it" print(msg4.format(-200))
# "," Add comma as thousand separator
msg5 = "The site has {:,} views"
print(msg5.format(3000000))
# "_" Add underscore as thousand separator
msg6 = "The site has {:_} views"
print(msg6.format(3000000))
# "d" convert to decimal integer
msg6 = "Site started with {:d} views"
print(msg6.format(3000))
# "e" convert number to exponential notation
msg7 = "The number is {:e}"
print(msg7.format(50000))
# octal, binary and hexadecimal format
print("octal: {0:o}, binary: {0:b}, hexadecimal: {0:x}".format(46))
If you observe the above example, we used different format types in placeholders { } to format the given string values based on requirements.
When you execute the above python program, you will get the result as shown below.
Hi guest, welcome
Hi guest , welcome
The amount is - 200, pay it
The site has 3,000,000 views
The site has 3_000_000 views
Site started with 3000 views
The number is 5.000000e+04
octal: 56, binary: 101110, hexadecimal: 2e
This is how you can use the string format() method in python to format the strings based on your requirements.
