#String Formatting
#str.format() is one of the string formatting methods in Python3, which allows multiple substitutions and value formatting. This method lets us concatenate elements within a string through positional formatting
#Dictionary
person={'name':'John','age':30}
sentence='My name is ' + person['name'] + ' and i am ' + str(person['age']) + ' yrs old.'
print(sentence)
#Above code using format method
sentence='My name is {} and i am {} yrs old.'.format(person['name'],person['age'])
print(sentence)
tag='h1'
text="This is a Heading"
sentence='<{0}>{1}<\{0}>'.format(tag,text)
print(sentence)
sentence='My name is {0[name]} and i am {0[age]} yrs old.'.format(person)
print(sentence)
class Person():
def __init__(self,name,age):
self.name=name
self.age=age
p1 = Person("Aron",25)
sentence='My name is {0.name} and i am {0.age} yrs old.'.format(p1)
print(sentence)
sentence='My name is {name} and i am {age} yrs old.'.format(name='ken',age=24)
print(sentence)
person={'name':'John','age':30}
#Best way to unpack a dictionary
sentence='My name is {name} and i am {age} yrs old.'.format(**person)
print(sentence)
for i in range(1,11):
sentence="The Value is {}".format(i)
print(sentence)
#To Print numbers in 2 digit only
for i in range(1,11):
sentence="The Value is {:02}".format(i)
print(sentence)
pi =3.14159265
sentence="Value of pi is {:.2f}".format(pi)
print(sentence)
sentence="1MB is equal to {} bytes".format(1000**2)
print(sentence)
sentence="1MB is equal to {:,} bytes".format(1000**2)
print(sentence)
sentence="1MB is equal to {:,.2f} bytes".format(1000**2)
print(sentence)
import datetime
my_date = datetime.datetime(2016,9,24,12,30,45)
print(my_date)
sentence='{:%B %d, %Y}'.format(my_date)
print(sentence)
#September 24, 2016 fell on a Saturday and was the 268 day of the year.
sentence="{0:%B %d, %Y} fell on a {0:%A} and was the {0:%j} day of the year.".format(my_date)
print(sentence)