Print Without Space in Python

Print without space in Python

Using the sep Parameter

Use the sep parameter to print without space in Python.

Python’s print() method displays a specified message or variables’ value on the screen or other standard output devices. Here, the message means a string or an object or object that will be transformed into a string before writing to the screen.

The print() method takes different parameters; one is the sep parameter we used above to print without space in Python. Let’s learn about these parameters with a brief description.

Parameter Description
object(s) Any object and n number of objects that you like to use but would be converted to a string before printing on the screen.
sep This parameter is optional; it represents a separator used to specify how the given objects (if there are many) will be separated. By default, it is a single whitespace.
end By default, the `print()` method prints a newline at the end; the end parameter is used to specify what to print at the end of the message. Is it a newline, empty string, a tab character, etc.
file This parameter is also optional and represents the object with the write method; by default, it is sys.stdout.
flush It is a Boolean value that specifies if an outcome is buffered (False) or flushed (True). By default, it is False. This parameter is also optional.

Let’s learn the end parameter below with an example.

Using the end Parameter

Use the end parameter to print without space in Python.

We have already learned about the end parameter in the previous section. This code also produced the same results, but we used the print() method thrice with the end parameter to print without space in Python.

Using + Operator

Use + operator to print without space in Python.

Here, we used the string concatenation operator represented with the + sign, used to concatenate the strings without any separator, but it would be challenging to use if we have many string values to concatenate. In that case, we can use the following solution.

Using join() Method

To print without space in Python:

First, we created a list containing string type values and named the list my_list. Next, we used the join() method to join all the list elements without space and saved the updated string in values_without_space variable, which was later passed to the print() method to display on the screen.

The join() method took an iterable (my_list) as an argument, iterated over all items of the given iterable and joined them in a single string. Remember, the join() method was chained with a string and treated as a separator; in the above example, it is an empty string.

That’s all about how to print without space in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *