Remove space between variable and string in Python

avatar

Borislav Hadzhiev

Last updated: Aug 20, 2022

banner

Photo from Unsplash

Remove space between variable and string in Python #

Use a formatted string literal to remove the space between a variable and a string, e.g. result = f'{variable} world'. The variable can be interpolated right next to the string when using a formatted string literal.

main.py
variable = 'hello' # ✅ using formatted string literal result = f'{variable} world' print(result) # 👉️ 'hello world' # ----------------------------------------- # ✅ using the addition (+) operator result = variable + ' world' print(result) # 👉️ hello world # ----------------------------------------- # ✅ using str.join() and str.split() variable = 'hello ' result = ' '.join(f'{variable} world'.split()) print(result) # 👉️ 'hello world' # ----------------------------------------- # ✅ remove unnecessary whitespace when printing variable = 'hello ' # 👇️ hello world! print(variable, 'world', '!', sep='')

The first example uses a formatted string literal to remove the space between a variable and a string.

main.py
variable = 'hello' # ✅ using formatted string literal result = f'{variable} world' print(result) # 👉️ 'hello world' result = f'{variable}world' print(result) # 👉️ 'helloworld'
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
variable = 'hello' variable_2 = 'world' result = f'{variable} {variable_2}!' print(result) # 👉️ 'hello world!'

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

Alternatively, you can use the addition (+) operator to concatenate the variable and the string without extra whitespace.

main.py
variable = 'hello' result = variable + ' world' print(result) # 👉️ 'hello world'
If you use the addition operator, you have to make sure that the values on the left and right-hand sides are of type string.

You can use the str() class if you need to convert a value to a string, e.g. str(my_number).

If you have a string that contains multiple spaces next to one another, you can use the str.split() and str.join() methods to remove the extra whitespace.

main.py
variable = 'hello ' result = ' '.join(f'{variable} world'.split()) print(result) # 👉️ 'hello world' result = ''.join(f'{variable}world'.split()) print(result) # 👉️ 'helloworld'

The str.split() method splits the string into a list of substrings using a delimiter.

If no delimiter is passed to the method, the method splits the string on one or more occurrences of whitespace characters.

main.py
variable = 'hello ' # 👇️ ['hello', 'world'] print(f'{variable} world'.split())

The last step is to use the str.join() method to join the list of strings.

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

The string the method is called on is used as the separator between the elements.

main.py
variable = 'hello ' result = ' '.join(f'{variable} world'.split()) print(result) # 👉️ 'hello world' result = ''.join(f'{variable}world'.split()) print(result) # 👉️ 'helloworld'

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.

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.