Any new developer has to know how to print a String
and a Variable
in Python. This is a basic technique to make the program friendly to the user. In this article, we will learn different ways to get this job done!
How to print a String and a Variable in Python
Below are different ways to print a String and a Variable in Python using the print() function.
Print a String and a Variable separately
You use the print()
function 2 times. 1 for printing the string
and 1 for the Variable
.
The syntax for printing a string
is like this:
print([string])
Make sure that the text of the string
is placed between ""
or ''
.
Here is the syntax for printing a Variable
:
print([variable])
You need to write the name of the Variable
in between the ()
of the function.
Below is a code sample:
# Print a String print("Learn Share It") # Create a Variable varNum = 12 # Print a Variable print(varNum)
The output will be:
Learn Share It
12
As you see, the Python program prints the String
and the Variable
on 2 lines. If you want these 2 to stay on the same lines, you need to customize the first print()
command with a second parameter.
Here is the syntax:
print([string], end=" ")
The end=" "
makes sure that the first print()
function will not break the line after completing printing. Instead, it ends the line with a space.
Here is the code sample:
# Print a String print("Learn Share It", end=" ") # Create a Variable varNum = 12 # Print a Variable print(varNum)
The output will be:
Learn Share It 12
Print the String and the Variable using only 1 print() function
Print()
function gives no limit for the parameter. You can add different parameters inside the ()
, separating each other with a ,
comma.
Here is the syntax:
print(parameter1, parameter2, …, parameterN)
The parameters can be Strings
or Variables
as you decide.
Here is the sample:
# Create a Variable varNum = 16 # Print a String print("Learn", "Share", "It", varNum)
The output will be:
Learn Share It 16
Print the String and the Variable using str-formatting
You can also use the string-formatting
to do this work. This way is convenient if you want to add a variable in the middle of a string.
The syntax is like this:
print([string {}].format(varNum))
You take advantage of the {}
to show where you want to print the Variable
in the string. The .format()
helps you determine which variable you need to print.
See the sample below:
# Create Variable varNum = 16 # Print a String print("Learn Share {} It".format(varNum))
The output will be:
Learn Share 16 It
Print the String and the Variable using f-string
The f-string
also helps you print the variable in the middle of a string. The syntax is like this:
print(f[string {[variable]}])
You still use the {}
to show where you want to print the variable
. But the great thing is, you can add the variable directly in between the {}
. Make sure that there is an f
before the string.
See the code sample below:
# Create Variable varNum = 16 # Print a String print(f"Learn Share {varNum} It")
The output will be:
Learn Share 16 It
Summary
To summarize, we have taught you 4 tips on how to print a String and a Variable in Python. You rely on the print() function to do the work. But each way has a unique syntax. You are free to choose the way that is the most convenient for your programming!

I am William Nguyen and currently work as a software developer. I am highly interested in programming, especially in Python, C++, Html, Css, and Javascript. I’ve worked on numerous software development projects throughout the years. I am eager to share my knowledge with others that enjoy programming!