Print a string and an integer together in Python

avatar

Borislav Hadzhiev

Last updated: Aug 29, 2022

banner

Photo from Unsplash

Print a string and an integer together in Python #

Use a formatted string literal to print a string and an integer together, e.g. print(f'The number is {my_int}'). Formatted string literals (f-strings) let us include expressions and variables inside of a string by prefixing the string with f.

main.py
my_int = 100 # ✅ print string and number using a formatted string literal result = f'The number is {my_int}' print(result) # 👉️ The number is 100 # -------------------------------------------------- # ✅ print string and number using addition (+) operator result = 'The number is ' + str(my_int) print(result) # 👉️ The number is 100 # -------------------------------------------------- # ✅ print string and number using comma # 👇️ The number is 100 print('The number is ', my_int, sep='')

The first example uses a formatted string literal to print a string and an integer together.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

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

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

Note that the print() function returns None, so don't try to store the result of calling print in a variable.
main.py
my_int = 1234567 # ⛔️ BAD (print always returns None) result = print(f'The number is {my_int}') print(result) # 👉️ None

Instead, store the value in a variable and pass the variable to the print() function.

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

Alternatively, you can use the addition (+) operator.

Print a string and an integer together using addition (+) operator #

To print a string and an integer together:

  1. Use the str() class to convert the integer to a string.
  2. Use the addition (+) operator to concatenate the two strings.
  3. Use the print() function to print the result.
main.py
my_int = 100 result = 'The number is ' + str(my_int) print(result) # 👉️ The number is 100

We used the str() class to convert the integer to a string.

This is necessary because the values on the left and right-hand sides of the addition (+) operator need to be of compatible types.

If you try to use the addition (+) operator with a string and an integer, you'd get an error.

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

To get around this, we have to use the str() class to convert the integer to a string.

When using formatted string literals, we don't have to explicitly convert the integer to a string because it's done for us automatically.

Print a string and an integer together using commas #

Pass multiple, comma-separated arguments to the print() function to print a string and an integer. The print() function will print the values next to one another separated by spaces.

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

We passed multiple, comma-separated arguments to the print() function to print a string and an integer.

You can use the sep keyword argument if you need to adjust the separator between the values.

By default, the sep argument is set to a space.

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

Which approach you pick is a matter of personal preference. I'd go with using a formatted string literal because they don't require conversion and are quite easy to read.

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.