Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

format function in python

  •  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.
Syntax:  str.format(values)
  • 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.

  1. s0="my name is {fname},I m {age}".format("fname=abc",age=19)
  2. s1="my name is{0},I m {1}".format("abc",19)
  3. s2="my name is {},I m {}".format("abc",19)
  4. print(s0)
  5. print(s1)
  6. print(s2)

Output:    my name is abc, I m 19.
                 my name is abc, I m 19.
                 my name is abc, I m 19.




This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

format function in python

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×