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.

 

Python string format method example result

 

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.

 

string.format(arg1, arg2, etc.)

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.

 

str1 = "{} to {}".format("Welcome", "Tutlane.com")
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.

 

Welcome to Tutlane.com
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.

 

# Positional arguments
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.

 

Welcome to Tutlane.com
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.

 

TypeDescription
< It will left align the result to the remaining space.
> It will right-align the result to the remaining space.
^ It will center align the result to the remaining space.
+ It is useful to indicate whether the result is positive or negative
- It is useful to indicate the result is negative
= It will place the sign (+/-) to the leftmost position.
_ It will be used as a thousands separator.
, It will be used as a thousands separator
b It will convert number to binary format
c It will convert number to Unicode character
d It will convert number to decimal format
e It will convert number to exponential notation (lowercase e)
E It will convert number to exponential notation (uppercase E)
f It will convert number to fixed-point number
F It's same as 'f', but it displays inf as INF and nan as NAN
g It will show in a general format
G It's same as 'g', but it will use upper case E for exponential notation
n It will convert to number format, and it's same as 'd'.
o It will convert number to octal format
x It will convert number to hexadecimal format (lower case)
X It will convert number to hexadecimal format (upper case)
% It will convert the number into a percentage format.

Following is the example of using formatting the strings using different format types in python.

 

# "<" left-align the value
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
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.