Ways to print without a space in Python

When working with Python, you may come across situations where you need to print output without a space. This can be useful for formatting output, creating command line interfaces, or printing data in a specific format. In this article, we will explore different ways to print without a space in Python.

Using the end parameter of the print function.

The simplest way to print without a space in Python is by using the end parameter of the print function. By default, the end parameter is set to “\n” which prints a new line character after the output. You can change this to an empty string to remove the space.

Here’s an example:

print("Foot", end="")
print("ball")

# Output : Football

In this example, we set the end parameter of the first print statement to an empty string, which removes the space between “Hello” and “World”.

Using the sep parameter of the print function.

Another way to print without a space is by using the sep parameter of the print function. The sep parameter specifies the separator between multiple items that are printed. By default, the separator is a space character, but you can change it to an empty string to remove the space.

Here’s an example:

print("Foot", "ball", sep="")

# Output : Football

In this example, we set the sep parameter to an empty string, which removes the space between “Hello” and “World”.

Using string concatenation

You can also print without a space by concatenating strings together before printing. This can be useful if you have multiple strings that you want to print without a space.

Here’s an example:

Foot = "Foot"
ball = "ball"
print(Foot + ball)

# Output : Football

In this example, we concatenate the strings “Hello” and “World” together before printing them.

Using the join method

If you have a list of strings that you want to print without a space, you can use the join method to join the strings together with an empty separator.

Here’s an example:

my_list = ["Foot", "ball"]
print("".join(my_list))
# Output : Football

In this example, we use the join method to join the strings in the list “my_list” together with an empty separator.

Conclusion

In this article, we explored different ways to print without a space in Python. Whether you’re using the end parameter of the print function, the sep parameter, string concatenation, or the join() method, you can easily remove spaces between your output in Python.

Explore more from this category at Python Misc. Alternatively, search and view other topics at All Tutorials.