Untitled Document
String Formatting
1. Traditional (Normal) way of printing strings Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****   
       # Traditional (Normal) way of printing strings 
       # here we will see the string concatenation and formating was done with '+' sign which becomes difficult 
       # to handle when we have to perform too many string variales and need some repetative formatting 
       
       my_dict={'name':"Mangesh", 'age':33}
       print ("The Name Of Person is :" + my_dict['name']  + " and age is :" + str(my_dict['age']))
    
       
2. Printing strings using format method Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Printing strings using format method

       my_dict={'name':"Mangesh", 'age':33}
       print("The Name of Person is : {} and age is : {}".format(my_dict['name'],my_dict['age']))
       
       # EX - 2 Printing sring with format method using numbers ( positinal parameters )
       
       my_dict={'name':"Mangesh", 'age':33}
       print("The Name of Person is : {0} and age is : {1}".format(my_dict['name'],my_dict['age']))
       
# EX - 3 More practical Example for above formatting example tag='h1' text='This is a headline' sentence ='<{0}>{1}'.format(tag,text) print(sentence) # EX - 4 Another (Multiple) ways of printing dictionary my_dict={'name':"Mangesh", 'age':33} print("My Name is {0[name]} and age is {1[age]}".format(my_dict,my_dict)) # OR print("My Name is {0[name]} and age is {0[age]}".format(my_dict)) # OR print("My Name is {name} and age is {age}".format(**my_dict))
3. Printing Oject Details Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Printing Oject Details
             
       class Person():
       	def __init__ (self,name,age):
       		self.name = name
       		self.age = age
       
       p1 = Person("Mrunmayee", "1")
       
       statement = "My Name is {0.name} and I am {0.age} years old".format(p1)
       print(statement)
       
       
Untitled Document