Format Strings in Python

In Python, we can not combine strings and number directly.
mynum=10
mystr="The price of a book is"
print(mystr+mynum)

But, we can combine strings and number using format() method. The format() method takes the passed arguments and places it into the respective placeholders in the string. The placeholders are denoted as {}
mynum=10
mystr="The price of a book is"
print(mystr+" {}".format(mynum))

Output will be
The price of a book is 10

We can also use index value inside {} to place the correct argument in the correct placeholder.
a="To"
b="Neoogy"
print("Welcome {1} {0}".format(b,a))

Output will be
Welcome To Neoogy


0 Comments