Using Python to print variable in strings

In this short tutorial, we look at how you can use Python print variable in strings. We look at all the various methods along with their limitations and caveats.

Table of Contents - Python Print Variables

  • Python print variables
  • Printing variables using f-string
  • Using string concatenation
  • Python print variables using a comma “,”
  • Limitations & Caveats - Python print variables
  • Other Related Concepts

Python print variables:

While printing strings, it is common to print a variable’s values inside it. A common example is you might want to print the first and last name in a sentence and these values are stored in two variables respectively.

These values cannot be hardcoded into the string and need to be passed dynamically, hence Python comes with a few inbuilt functions that allow you to print variable values.

Printing variables using f-string:

Using f-strings in Python to print variables is the most commonly used method and I would personally recommend using this method.

In this method, an ‘f’ is placed before the opening quotation mark of a string. Braces {} are placed around the names of variables that you are looking to print. Python replaces these variables with their values when the code is executed and the string is displayed.

Such strings are called f-strings. Python further allows you to format these strings by using methods like ‘.upper’ or ‘.title’ etc.

Code - f-string

language = "Python"

#Adding the language variable into a string
print(f"Hello {language} World!")

#Output = “Hello Python World!”

Note: These f-strings were introduced only in Python 3.6. In case you are using Python versions earlier than that you would have to use the ‘format()’ method. The syntax for that is as follows:

Python Print Variable - format() Code

language = "Python"

#Adding the language variable into a string
print("Hello {} World!".format(language))

#Output = “Hello Python World!”

Using string concatenation:

String concatenation is a method that can be used to add strings together. This is done by using the “+” character between two variables or strings. This way we can use Python to print variable values along with the string.

Python Print Variable - String Concatenation:

language = "Python"

#Adding the language variable into a string
print("Hello" +" "+ language +" "+ "World!")

#Output - “Hello Python World!”

While using this method, spaces are not added and you would have to manually add them and hence this method is not used very often.

Python print variables using a comma “,”:

This method is another commonly used method in Python to print variables. This method is quite similar to the string concatenation method; however, here we use “,” between the variables.

Additionally the “,” itself adds a space to the string and you would not have to add it.

Python Print Variable - Using “,”:

language = "Python"

#Adding the language variable into a string
print("Hello", language, "World!")

#Output - "Hello Python World!"

Limitations & Caveats - Python print variables

  • While using the first method do keep your version of PYthon in mind as f-strings would only work on Python 3.6 and above
  • The string concatenation method is not widely used however feel free to try it out for your better understanding
  • While using the “,” methods be careful not to add spaces between variables as the comma itself adds them for you
  • I would recommend using methods 1 & 2, you could try them and choose whichever method you are comfortable with