Mostly I work with Python 3. There can I write this:
print(f"The answer is {21 + 21}!")
Output:
The answer is 42!
But in Python 2, f-strings do not exist. So is the following the best way?
print("the answer is " + str(21 + 21) + "!")
Mostly I work with Python 3. There can I write this:
print(f"The answer is {21 + 21}!")
Output:
The answer is 42!
But in Python 2, f-strings do not exist. So is the following the best way?
print("the answer is " + str(21 + 21) + "!")
Actually, you can use the .format() method for readability, and it is the where the f-string comes from.
You can use:
print("the answer is {}!".format(21+21))
formatexists in 2.7