String {} and format() method in Python [Explained]

There are different ways to use the format method in Python:

I- using named indexes:

way1 = "Im {fname},and my age is {age}".format(fname = "John", age = 36)
print(way1)

Output:

Im John,and my age is 36




II- Using  numbered indexes

way2 = "Im {0}, and my age is{1}".format("John",36)
print(way2)

Output:

Im John,and my age is 36




II- Using  empty {}, with order:

way3 = "Im {}, and my age is {}".format("John",36)
print(way3)

Output:

Im John,and my age is 36
Bilel

Bilel

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x