- The format() function is used to format a specific value and insert that value in string placeholders.
- The placeholders are cost{20}, number index{1},empty{}
- The place holder is defined by" curly braces"{}.
- The format function consists of parameters called value.
- It returns the formatted string.
- Inside the placeholders, we can add different formatting types
- :+ indicates positive values.
- : - indicates negative values.
- : indicates the space before positive and also negative numbers.
- :_ indicates as a thousand separator.
- :, uses as comma thousand separator.
- := left most position.
- :< result in left alignment.
- :> result in right alignment.
- :^ result in the center.
- :% percentage format.
- :b Binary format.
- :c Unicode format.
- :d Decimal format.
- :e format with lower case.
- :E format with upper case.
- :f format with fixpoint number.
- :o format is octal.
- :x format is Hexa.
- :X format is Hexa in uppercase.
- :n number format.
#write a python code to demonstrate format function.
- s0="my name is {fname},I m {age}".format("fname=abc",age=19)
- s1="my name is{0},I m {1}".format("abc",19)
- s2="my name is {},I m {}".format("abc",19)
- print(s0)
- print(s1)
- print(s2)
Output: my name is abc, I m 19.
my name is abc, I m 19.
my name is abc, I m 19.
No comments