String Formatting

String Formatting – using Place Holder {}

my_txt = "Deals under {price} dollars!!!".format(price = 99)
print(my_txt)

# more example -- Type 1
person_details = 'Name: {name} \nAge: {age}'.format(name = 'Foo Baar', age = 20)
print(person_details,' -- Type 1')

# more example -- Type 2
person_details = 'Name: {} \nAge: {}'.format('Foo Baar',20)
print(person_details,' -- Type 2')

# more example -- Type 3
person_details = 'Name: {0} \nAge: {1}'.format('Foo Baar',20)
print(person_details,' -- Type 3')

# more example -- Type 4
person_details = 'Name: {1} \nAge: {0}'.format(29,'Foo Baar')
print(person_details,' -- Type 4')

using %-formatting

price = 99
my_txt = "Deals under %s dollars!!!" % price
print(my_txt)

# more example
name = 'Foo Baar'
age = 20
person_details = 'Name: %s \nAge: %s' % (name, age)
print(person_details)

usingĀ  f-formatting

price = 99
my_txt = f"Deals under {price} dollars!!!" #using f formatting
print(my_txt)

# more example
name = 'Foo Baar'
age = 20
person_details = f'Name: {name} \nAge: {age}'
print(person_details)

# example of printing in one line
print(f'{2**10}') #using f formatting
print(type(f'{2**10}')) # checking the type of output -- String