How to print a String and a Variable in Python

avatar

Borislav Hadzhiev

Last updated: Sep 3, 2022

banner

Photo from Unsplash

Print a String and a Variable in Python #

To print a string and a variable:

  1. Use a formatted string literal to interpolate the variable in the string.
  2. Use the print() function to print the result.
  3. For example, print(f'hello {variable}').
main.py
variable = 'world' # ✅ print string and variable using f-string result = f'hello {variable}' print(result) # 👉️ 'hello world' # ------------------------------------------------ # ✅ print string and variable using addition (+) operator result = 'hello ' + variable print(result) # 👉️ 'hello world' # ------------------------------------------------ # ✅ print string and variable using comma print('hello', variable) # 👉️ 'hello world'

We used a formatted string literal to interpolate a variable in a string and used the print() function to print the result.

The print function takes one or more objects and prints them to sys.stdout.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
my_str = 'is subscribed:' my_bool = True result = f'The user {my_str} {my_bool}' print(result) # 👉️ The user is subscribed: True

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals automatically take care of converting the variable to a string.

main.py
my_str = 'The number is:' my_int = 100 result = f'{my_str} {my_int}' print(result) # 👉️ The number is: 100

If you use the addition (+) operator to print a string and a variable, you have to make sure the values on the left and right-hand sides of the operator are of compatible types.

Print a String and a Variable using the addition (+) operator #

To print a string and a variable:

  1. Use the addition (+) operator to concatenate the string and the variable.
  2. Use the print() function to print the result.
  3. For example, print('hello' + variable).
main.py
variable = 'world' result = 'hello ' + variable print(result) # 👉️ 'hello world'

Using the addition (+) operator is not as intuitive as using an f-string because the values on the left and right-hand sides need to be of compatible types.

main.py
my_str = 'The number is:' my_int = 100 # ⛔️ TypeError: can only concatenate str (not "int") to str result = my_str + my_int print(result)

The code sample causes an error because we're trying to concatenate an integer to a string.

To get around this, we have to convert the integer to a string before using the addition (+) operator.

main.py
my_str = 'The number is:' my_int = 100 result = my_str + ' ' + str(my_int) print(result) # 👉️ The number is: 100

You can use the addition (+) operator as many times as necessary when concatenating strings.

The example also adds a space between the string and the variable before printing the result.

You can also pass multiple, comma-separated arguments to the print() function.

main.py
my_str = 'The number is:' my_int = 100 print(my_str, my_int, '!') # 👉️ The number is: 100 !

Notice that we can print a string and a variable storing an integer on the same line when passing multiple, comma-separated arguments to print().

By default, when you pass multiple, comma-separated arguments to the print() function, they get separated by a space.

You can set the sep keyword argument to an empty string to remove the separator.

main.py
my_str = 'The number is:' my_int = 100 # 👇️ The number is: 100! print(my_str, ' ', my_int, '!', sep='')

By setting the argument to an empty string, no extra whitespace is added between the values.

Alternatively, you can use the str.format() method.

Print a String and a Variable using str.format() #

To print a string and a variable:

  1. Use the str.format() method to interpolate the variable in the string.
  2. Use the print() function to print the result.
  3. For example, print("{} + {} is {}".format(my_int1, my_int2, result)).
main.py
my_int1 = 100 my_int2 = 200 result = my_int1 + my_int2 result = "{} + {} is {}".format(my_int1, my_int2, result) print(result) # 👉️ "100 + 200 is 300"

The str.format method performs string formatting operations.

The string the method is called on can contain replacement fields specified using curly braces {}.

Make sure to provide exactly as many arguments to the format() method as you have replacement fields in the string.

The string in the example has 3 replacement fields, so we passed 3 arguments to the str.format() method.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.