Python String Example | Format a String using format() Function

This python string example code will show us how to use string format() function to format a python string.

name = 'NoteHope'
year = 2014

string = '{} was started in {}'.format(name, year)
print(string)

string = '{0} was started in {1}'.format(name, year)
print(string)

yw = {'name': 'YoungWonks', 'year': 2014}
string = "{name} was started in {year}.".format(name=yw['name'], year=yw['year'])
print(string)

Run this code, we will get:

NoteHope was started in 2014
NoteHope was started in 2014
NoteHope was started in 2014