Python
In this tutorial we will learn about the string format method in Python.
In Python we get the format
method which allows us to format string by substituting multiple values at different positions in the string.
This works by putting {}
in a string and then calling the format()
method.
{}.format(value)
Where {}
is a placeholder which is substituted by the value
.
The value
can be a character, string, integer, floating point number and even a variable.
In the following example we are formatting the given string.
# string format
str = "Hello {}".format("World")
# output
print(str) # Hello World
In the above code the value World
will be insert at {}
position in the string "Hello {}"
.
In the following example we are substituting the value of a variable in the given string.
# string
s = "World"
# string format
str = "Hello {}".format(s)
# output
print(str) # Hello World
We can place multiple {}
in a string and then substitute more that one values using the format
method.
{}{}{}.format(value1, value2, value3)
In the above syntax we are substituting 3 values at three positions.
In the following example we are substituting 3 values in the given string.
# string formatting
str = "The quick {} fox {} over {} lazy dog.".format("brown", "jumps", "the")
# output
print(str)
We will get the following output for the above code.
The quick brown fox jumps over the lazy dog.
We can set position numbers like {i}
and it will be substituted by the value at ith position in the format
method.
{0}{1}{2}.format(value0, value1, value2)
In the above syntax value0
will be inserted at the placeholder {0}
.
value1
will be inserted at the placeholder {1}
and value2
will be inserted at the placeholder {2}
.
Index number starts from 0.
In the following example we are substituting values at two positions.
# string formatting
str = "The quick brown {0} jumps over the lazy {1}.".format("fox", "dog")
# output
print(str)
In the above Python program the value fox
is inserted at position {0}
and the value dog
is inserted at the position {1}
.
In the following example we are substituting values at three positions.
# string formatting
str = "The quick {2} {0} jumps over the lazy {1}.".format("fox", "dog", "brown")
# output
print(str) # The quick brown fox jumps over the lazy dog.
The insertion occurs in the following order.
{0} --> fox
{1} --> dog
{2} --> brown
The value
is substituted at the position
after going through the conversion
.
{position:conversion}.format(value)
And following are some of the conversion type.
Conversion | Description |
---|---|
s | String |
d | Integers (base 10) |
f | Floating point numbers |
b | Binary |
o | Octal |
x | Hexadecimal with lower case letters |
X | Hexadecimal with upper case letters |
e | Exponent notation |
In the following Python program were are substituting the values using format method.
print("Hello {0:s}!!!".format("World")) # Hello World!!!
print("This is an integer {0:d}".format(10)) # This is an integer 10
print("Float value {0:f}".format(123.4567)) # Float value 123.456700
print("Float value {0:.2f}".format(123.4567)) # Float value 123.46
print("Binary value {0:b}".format(12)) # Binary value 1100
print("Octal value {0:o}".format(12)) # Octal value 14
print("Hex lower case {0:x}".format(29)) # Hex lower case 1d
print("Hex upper case {0:X}".format(29)) # Hex upper case 1D
The {0:.2f}
inserts a floating point value with 2 decimal place.
ADVERTISEMENT