Python 3.6
onwards f-strings are introduced. They allow us to place the variables inside strings more conveniently. This is used to format strings.
Conventional way of writing strings with variables
name = 'Sarang'
country = 'India'
string = 'Hey! I am '+ name +' and I am from '+country+'.'
print(string)
Output
Hey! I am Sarang and I am from India.
name = 'Sarang'
country = 'India'
string = 'Hey! I am', name,'and I am from',country,'.'
# as string is now tuple - we need to convert it to string
string = ' '.join(string)
print(string)
Output
Hey! I am Sarang and I am from India.
name = 'Sarang'
country = 'India'
string = ['Hey! I am', name,'and I am from',country,'.']
string = ' '.join(string)
print(string)
Output
Hey! I am Sarang and I am from India.
name = 'Sarang'
country = 'India'
string = 'Hey! I am {} and I am from {}'.format(name, country)
print(string)
Output
Hey! I am Sarang and I am from India.
name = 'Sarang'
country = 'India'
string = f'Hey! I am {name} and I am from {country}'
print(string)
Output
Hey! I am Sarang and I am from India.
Click here for more examples on f-strings