Print Without Space in Python

Print without space in Python

Using the sep Parameter

Use the sep parameter to print without space in Python.

print("How", "are", "you?", sep='')
Howareyou?

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.

[wpdatatable id=17]

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

Using the end Parameter

Use the end parameter to print without space in Python.

print("How", end='')
print("are", end='')
print("you?", end='')
Howareyou?

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.

print("How" + "are" + "you?")
Howareyou?

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:

my_list = ["How", "are", "you?"]
values_without_space = ''.join(my_list)
print(values_without_space)
Howareyou?

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.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.