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.
You can use the print() function in Python and use the + operator to concatenate a string and a variable. For example:
Concatenate a string and a variable in Python using the + operator
name = "John"
age = 20
print("My name is " + name + " and I am " + str(age) + " years old.")
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Python - The Practical Guide</div>
Alternatively, you can use the format() method to insert the value of a variable into a string, like this:
Concatenate a string and a variable in Python using the format method
name = "John"
age = 20
print("My name is {} and I am {} years old.".format(name, age))or use f-strings (Python 3.6+)
Concatenate a string and a variable in Python using the f-strings
name = "John"
age = 20
print(f"My name is {name} and I am {age} years old.")You can put all these examples in the same program and will run successfully.