Print a string and an integer in Python

avatar

Borislav Hadzhiev

Last updated: Sep 3, 2022

banner

Photo from Unsplash

Print a string and an integer in Python #

Use a formatted string literal to print a string and an integer, e.g. print(f'The integer 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 = 247 # ✅ print string and integer on same line result = f'The integer is {my_int}' print(result) # 👉️ "The integer is 247" # -------------------------------------------------- # ✅ print string and integer using addition (+) operator result = 'The integer is ' + str(my_int) print(result) # 👉️ "The integer is 247" # -------------------------------------------------- # ✅ print string and integer using comma # 👇️ The integer is 247 print('The integer is ', my_int, sep='')

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

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
my_int = 247 result = f'The integer is {my_int}' print(result) # 👉️ "The integer is 247"

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.

Formatted string literals take care of converting the integer to a string and make it quite easy to interpolate variables of any type in strings.

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

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

To print a string and an integer:

  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 = 247 result = 'The integer is ' + str(my_int) print(result) # 👉️ "The integer is 247"

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 compatible types.

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

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

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.

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

Print a string and an integer using str.format() #

To print a string and an integer:

  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("The integer is {}".format(my_int)).
main.py
my_int = 247 result = "The integer is {}".format(my_int) print(result) # 👉️ "The integer is 247"

The str.format method performs string formatting operations.

main.py
first = 'James' last = 'Doe' result = "His name is {} {}".format(first, last) print(result) # 👉️ "His name is James Doe"

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 str.format() method takes care of automatically converting the integer to a string.

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.