Using the sep
Parameter
Use the sep
parameter to print without space in Python.
1 2 3 |
print("How", "are", "you?", sep='') |
1 2 3 |
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.
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.
1 2 3 4 5 |
print("How", end='') print("are", end='') print("you?", end='') |
1 2 3 |
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.
1 2 3 |
print("How" + "are" + "you?") |
1 2 3 |
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.
Further reading:
Using join()
Method
To print without space in Python:
- Create a list of string-type values.
- Use the
join()
method to join all the list values without space.
1 2 3 4 5 |
my_list = ["How", "are", "you?"] values_without_space = ''.join(my_list) print(values_without_space) |
1 2 3 |
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.