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-A

Enthusiastic Network Engineer specializing in Cisco ACI, passionate about solving challenges. A lifelong learner who loves gaining and sharing knowledge. Profile: https://www.linkedin.com/in/bilel-ameur-71116b2b5
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x