How to add space between variables in Python


Borislav Hadzhiev

Last updated: Jun 21, 2022


Check out my new book

Add space between variables in Python #

Use a formatted string literal to add a space between variables in Python, e.g. result = f'{var_1} {var_2}'. Formatted string literals allow us to include expressions inside of a string by prefixing the string with f.
var_1 = 'hello' var_2 = 123 result = f'{var_1} {var_2}' print(result) # 👉️ hello 123

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
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}.

You can use this approach to add space between as many variables as necessary.

Alternatively, you can use the str.join() method to add space between variables.
var_1 = 'hello' var_2 = 123 result_2 = ' '.join(map(str, [var_1, var_2])) print(result_2) # 👉️ hello 123

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

If your list of variable contains numbers or other types, convert all of the values to string before calling join().

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

We used the map() function to convert the integer stored in var_2 to a string but this isn't necessary if you are only joining strings.
var_1 = 'hello' var_2 = 'world' result_2 = ' '.join([var_1, var_2]) print(result_2) # 👉️ hello world

Alternatively, you can use the str.format() method.
var_1 = 'hello' var_2 = 123 result = '{} {}'.format(var_1, var_2) print(result) # 👉️ 'hello 123'

The str.format method performs string formatting operations.
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 {}.

You can also use the addition (+) operator to add a space between two variables, but make sure they are of compatible types.
var_1 = 'hello' var_2 = 123 result = var_1 + ' ' + str(var_2) print(result) # 👉️ 'hello 123'

Notice that we used the str() class to convert the integer to a string so we can concatenate the variables with a space in between.

If you need to add multiple spaces between variables, use the multiplication operator to make your code more readable.
var_1 = 'hello' var_2 = 123 result = var_1 + ' ' * 3 + str(var_2) print(repr(result)) # 👉️ 'hello 123'

The multiplication operator can be used to repeat a string a specified number of times.
print(repr(' ' * 3)) # 👉️ ' ' print(repr('a' * 3)) # 👉️ 'aaa'
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.