How can I print variable and string on same line in Python?
You can use the print()
function in Python and use the +
operator to concatenate a string and a variable. For example:
name = "John"
age = 20
print("My name is " + name + " and I am " + str(age) + " years old.")
Watch a video course
Python - The Practical Guide
Alternatively, you can use the format()
method to insert the value of a variable into a string, like this:
name = "John"
age = 20
print("My name is {} and I am {} years old.".format(name, age))
or use f-strings (Python 3.6+)
You can put all these examples in the same program and will run successfully.