Python Print Without Space

Rate this post

Problem Formulation

Python’s print() function allows an arbitrary number of comma-separated values and prints them to the shell, separated by a single empty space character ‘ ‘.

The following example shows how you pass four string values as arguments into the print() function:

>>> print('learn', 'python', 'with', 'finxter')
learn python with finxter

The resulting shell output has an added empty space character ' ' to separate those four values.

How to print without the extra space?

Solution With Separator Argument

To print multiple values or variables without the default single space character in between, use the print() function with the optional separator keyword argument sep and set it to the empty string ''. For example, the statement print('hello', 'world', sep='') prints helloworld without the added empty space separator character.

>>> print('learn', 'python', 'with', 'finxter', sep='')

Per default, the separator keyword argument is set to the empty space sep=' '. You can also set it to any other string such as sep='-foo-' to obtain the following code:

>>> print('learn', 'python', 'with', 'finxter', sep='-foo-')

To learn more about the print function and its not very well-known arguments, feel free to watch my explainer video here:

Solution Without Separator Argument

To print multiple values or variables without the default single space character in between without explicitly overwriting the default separator argument, merge the multiple values using string concatenation before printing a single string. For example, the statement print('hello' + 'world') prints helloworld without the added empty space separator character.

>>> print('learn' + 'python' + 'with' + 'finxter')

While this solution doesn’t need a separator argument and a comma-separated argument list, it does need the plus operator + to concatenate two strings repeatedly until only one string is left. This can be tedious to write—and it may not be the most efficient solution due to the repeated creation of a new string based on two old string objects.

Solution to Print List of Strings Without Empty Space

To print a list of strings without an empty space as a separator, you have two options:

  • Use the separator argument sep='' like so: print(*str_list, sep='')
  • Merge the list into a single string using string.join() like so: print(''.join(str_list))

You can find the first way using unpacking here:

>>> str_list = ['learn', 'python', 'with', 'finxter']
>>> print(*str_list, sep='')

And the second way using string.join() here:

>>> str_list = ['learn', 'python', 'with', 'finxter']
>>> print(''.join(str_list))

To learn more about this, feel free to read my tutorial on the string.join() method.

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!