format()

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
bBinary format , base 2
cunicode chars
dDecimal format, base 10
oOctal format, base 8
xHex format, base 16 , lower case char
XHex format, base 16 , Upper case char
nSame as d but uses local settings for separator char
noneSame 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
spaceShow 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
All Built in Functions in Python
max() Bitwise operators using format() int() float()



plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer