placeholder : Marked by {}, Our data is placed inside this by using the format.
Using string
Just place the given string at the placeholder.
str='Welcome {} plus2net'
print(str.format('to')) # Welcome to plus2net
Using more than one placeholder
str='Product : {} , Prise is Rs {}'
print(str.format('Mango','45'))
Output
Product : Mango , Prise is Rs 45
using indexes for placeholders
We can mark the positions for the placeholders
str='Price is Rs : {1} , Product {0}'
print(str.format('Mango','45'))
Output
Price is Rs : 45 , Product Mango
Alignment of formatted text
We kept one placeholder of 25 positions. We have one product name ‘Mango’ to display within this 25 position width. We will align our output in left , centre and right side of the available 25 position width.
str='Left Aligned Product : {:<25},Price:45'
print(str.format('Mango'))
str='Right Aligned Product : {:>25},Price:45'
print(str.format('Mango'))
str='Center Aligned Product: {:^25},Price:45'
print(str.format('Mango'))
Output
Left Aligned Product : Mango ,Price:45
Right Aligned Product : Mango,Price:45
Center Aligned Product: Mango ,Price:45
Integer Presentation types
b | Binary format , base 2 |
c | unicode chars |
d | Decimal format, base 10 |
o | Octal format, base 8 |
x | Hex format, base 16 , lower case char |
X | Hex format, base 16 , Upper case char |
n | Same as d but uses local settings for separator char |
none | Same as d |
str='Binary value from decimal is: {:b}'
print(str.format(10))
str='HEX value from decimal is {:X}'
print(str.format(30))
str='HEX value from decimal is {:x}'
print(str.format(30))
str='Octal value from decimal is {:o}'
print(str.format(30))
Output is here
Binary value from decimal is: 1010
HEX value from decimal is 1E
HEX value from decimal is 1e
Octal value from decimal is 36
Using decimal place
We can format to display 4 decimal places
str='Price is Rs :{my_price:.4f}'
print(str.format(my_price=120.75))
Output
Price is Rs :120.7500
Total space we can define along with decimal place. Note that in aboe code we used 8 positions including the decimal ( dot ) . Now we will format for 9 places ( in total including integer , dot and decimal place )
str='Price is Rs :{my_price:9.4f}'
print(str.format(my_price=120.75))
Output ( total 9 places so one place is left blank at left side of the value )
Price is Rs : 120.7500
Sign type for numbers
This is applicable only for numbers
+ | Show both + and - signs |
- | Show only - signs |
space | Show leading space for + and - signs for negative numbers |
str="Sign numbers is {:-}"
print(str.format(-30))
str="Sign numbers is {:-}"
print(str.format(+30))
str="Sign numbers is {:+}"
print(str.format(30))
str="Sign numbers is {:+}"
print(str.format(-30))
str="Sign numbers is {: }"
print(str.format(30))
str="Sign numbers is {: }"
print(str.format(-30))
Output
Sign numbers is -30
Sign numbers is 30
Sign numbers is +30
Sign numbers is -30
Sign numbers is 30
Sign numbers is -30
Formatting for 1000 place
One comma to be added for every thousand place. Here is the input with output.
def format_number(number):
return ("{:,}".format(number))
print(format_number(1000000)) # 1,000,000
«All Built in Functions in Python
« max()
Bitwise operators using format() »
int()
float()
← Subscribe to our YouTube Channel here