In [1]:
first_name='kushagra'
last_name='gupta'
In [2]:
#Using Format method
sentence= 'My name is {} {}'.format(first_name,last_name)
print(sentence)
My name is kushagra gupta
In [4]:
#using F strings. Note you need python 3.6 or above to run f strings 
sentence= f'My name is {first_name.upper()} {last_name.upper()}'
print(sentence)
#[sweet]
My name is KUSHAGRA GUPTA
In [5]:
#Using Format Method
person={'name':'ritika','age':18}
sentence='My name is {} and I am {} years old'.format(person['name'],person['age'])
print(sentence)
My name is ritika and I am 18 years old
In [6]:
#Using F Strings
person={'name':'ritika','age':18}
sentence=f'My name is {person["name"]} and I am {person["age"]} years old'
print(sentence)
My name is ritika and I am 18 years old
In [7]:
#you can't use same type of quotes as they will conflict. or you have to use escape sequences.
person={'name':'ritika','age':18}
sentence=f'My name is {person['name']} and I am {person['age']} years old'
print(sentence)
  File "<ipython-input-7-4a3b3671d80a>", line 2
    sentence=f'My name is {person['name']} and I am {person['age']} years old'
                                      ^
SyntaxError: invalid syntax
In [8]:
#you can do Calculation
calculation =f'4 times 11 is equal to {4 *11}'
print(calculation)
4 times 11 is equal to 44
In [11]:
#Advance formatting using F Strings
for n in range(1,11):
    sentence=f'The value is {n:02}'    #zero padding by 2
    print(sentence)
The value is 01
The value is 02
The value is 03
The value is 04
The value is 05
The value is 06
The value is 07
The value is 08
The value is 09
The value is 10
In [13]:
pi =3.14159265
sentence=f'Pi is equal to {pi:0.4f}'   #Rounding the floating value to 4 places.
print(sentence)
Pi is equal to 3.1416
In [14]:
from datetime import datetime

birthday=datetime(2000,9,16)

sentence=f'Ritika has a birthday on {birthday}'
print(sentence)
Ritika has a birthday on 2000-09-16 00:00:00
In [18]:
from datetime import datetime

birthday=datetime(2000,11,16)

sentence=f'Ritika has a birthday on {birthday:%b %d, %Y}'
print(sentence)
Ritika has a birthday on Nov 16, 2000