Print a string and an integer on the same line in Python

avatar

Borislav Hadzhiev

Last updated: Sep 3, 2022

banner

Photo from Unsplash

Print a string and an integer on the same line in Python #

To print a string and an integer on the same line:

  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'The winner is {my_int}').
main.py
my_int = 842 # ✅ print string and integer on the same line (f-string) result = f'The winner is {my_int}' print(result) # 👉️ The winner is 842 # -------------------------------------------------- # ✅ print string and integer on the same line (+ operator) result = 'The winner is ' + str(my_int) print(result) # 👉️ The winner is 842 # -------------------------------------------------- # ✅ print string and integer on same line (sep) # 👇️ The winner is 842 print('The winner is ', my_int, sep='')

The first example uses a formatted string literal to print a string and an integer on the same line.

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

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 automatically converting the integer to a string.

Alternatively, you can call the print() function with multiple, comma-separated arguments.

Print a string and an integer on the same line using commas #

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

main.py
# 👇️ The winner is842 print('The winner is', my_int, sep='') # 👇️ The winner is 842 print('The winner 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.

main.py
print('a', 'b', 'c', sep="_") # 👉️ "a_b_c" print('a', 'b', 'c', sep="_", end="!!!") # 👉️ "a_b_c!!!"

The string we passed for the end keyword argument is inserted at the end of the string.

The end argument is set to a newline character (\n) by default.

Print a string and an integer on the same line using + operator #

To print a string and an integer on the same line:

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

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 print('Result: ' + 123)

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.

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.