Monday, 24 June 2019

formatting the strings

""" Python Program for
 Formatting of Strings """

# Default order
>>>a = "{} {} {}".format('Rama', 'Sita', 'Ravi')
>>>print(a)

# Positional Formatting
>>>a = "{1} {0} {2}".format('Rama', 'Sita', 'Ravi') #positional order
>>>print(a)

# Keyword Formatting
>>>a = "{v} {s} {r}".format(r = 'Rama', s = 'Sita', v = 'Ravi')
>>>print(a)

# Formatting of Integers
>>>a = "{0:b}".format(16)
>>>print("\nBinary representation of 16 is ")
>>>print(a)

# Formatting of Floats
>>>a = "{0:e}".format(165.6458)
>>>print("\nExponent representation of 165.6458 is ")
>>>print(a)

# Rounding off Integers
>>>a = "{0:.2f}".format(1/6)
>>>print("\none-sixth is : ")
>>>print(a)

# String alignment
>>>a = "|{:<10}|{:^10}|{:>10}|".format('Rama', 'Sita', 'Ravi')
>>>print("\nLeft, center and right alignment with Formatting: ")
>>>print(a)

"""output:
Rama Sita Ravi
Sita Rama Ravi
Ravi Sita Rama

Binary representation of 16 is
10000

Exponent representation of 165.6458 is
1.656458e+02

one-sixth is :
0.17

Left, center and right alignment with Formatting:
|Rama      |   Sita   |      Ravi|
"""



Python Programs



No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(...