Print variables without spaces between them in Python

avatar

Borislav Hadzhiev

Last updated: Aug 29, 2022

banner

Photo from Unsplash

Print variables without spaces between them in Python #

Use a formatted string literal to print variables without spaces, e.g. print(f'hello {variable_1}!'). Formatted string literals (f-strings) let us include expressions and variables inside of a string by prefixing the string with f.

main.py
variable_1 = 'world' # ✅ print values without spaces using formatted string literal print(f'hello {variable_1}!') # 👉️ 'hello world!' # ------------------------------------------ # ✅ print values without spaces using addition (+) operator print('hello ' + variable_1 + '!') # 👉️ 'hello world!' # ------------------------------------------ # ✅ print values without spaces using sep argument print('hello ', variable_1, '!', sep='') # 👉️ 'hello world!'

The first example uses a formatted string literal to print values without spaces between them.

main.py
variable_1 = 'world' print(f'hello {variable_1}!') # 👉️ 'hello world!'

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'{my_str} {my_bool}' print(result) # 👉️ 'is subscribed: True'

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

If you need to remove the leading and trailing spaces from a string, use the str.strip() method.

main.py
my_str = ' hello ' result = my_str.strip() print(repr(result)) # 👉️ 'hello'

The str.strip method returns a copy of the string with the leading and trailing whitespace removed.

The method doesn't change the original string, it returns a new string. Strings are immutable in Python.

You can call the str.strip() method directly in a formatted string literal.

main.py
variable_1 = ' world ' print(f'hello {variable_1.strip()}!') # 👉️ 'hello world!'

Alternatively, you can use the sep keyword argument.

Print variables without spaces between using sep argument #

Use the sep argument of the print() function to print values without spaces between them, e.g. print('hello ', variable_1, '!', sep=''). The sep argument can be set to an empty string to print without spaces between the values.

main.py
variable_1 = 'world' print('hello ', variable_1, '!', sep='') # 👉️ 'hello world!'

If you need to remove extra spaces when using the print() function, set the sep keyword argument to an empty string.

main.py
variable = 'hello ' # 👇️ hello world! print(variable, 'world', '!', sep='')

The sep keyword argument is 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.

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

Print variables without spaces between using addition (+) operator #

Use the addition operator to print variables without spaces, e.g. print('hello ' + variable_1 + '!'). The addition (+) operator will print the variables without adding extra space between them.

main.py
variable_1 = 'world' print('hello ' + variable_1 + '!') # 👉️ 'hello world!'

The addition (+) operator concatenates the values without adding extra space between them.

However, note that the values on the left and right-hand sides need to be of compatible types.

If you try to concatenate values of incompatible types, you'd get an error.

main.py
# ⛔️ TypeError: can only concatenate str (not "int") to str print('abc' + 123)

To get around this, you have to convert one of the values.

main.py
print('abc' + str(123)) # 👉️ 'abc123'

When using a formatted string literal, we don't have to make sure the values are of compatible types because f-strings automatically convert the values to strings for us.

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.