Python Forum

Full Version: Remove a space between a string and variable in print
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, i was wondering on how can i remove the space between the variable color and a string ", I prefer red"
color = str.lower(input ("Please enter your favorite color "))
print ("I don't like", color,", I prefer red")
the output of the code when purple is entered:
Output:
Please enter your favorite color purple I don't like purple , I prefer red
I hope to remove the space between the color and comma so it'll appear as
Quote:I don't like purple, I prefer red
Here is one way

import re
string = "I don't like purple , I prefer red."
string = re.sub('[\s],', ',', string)
print(string)
Output:
Python - fun.py:3 I don't like purple , I prefer red. I don't like purple, I prefer red.
(Jul-27-2022, 05:58 AM)sie Wrote: [ -> ]Hello, i was wondering on how can i remove the space between the variable color and a string ", I prefer red"

Possibly, don't introduce it; that way you don't have to remove it:

print (f"I don't like {color}, I prefer red.")
You could use the '.lower()' method in the output...

color = input ("Please enter your favorite color ")
print (f"I don't like {color.lower()}, I prefer red.")
... which leaves the original input as is. There are cases when that is preferable.
Isn't Python marvellous, it gives us many different ways to kill a cat!

print ("I don't like " + colour + ", I prefer red.")
Watch out for the apostrophe in don't, can cause trouble sometimes. In that case esape it: \'
(Jul-27-2022, 07:27 AM)Pedroski55 Wrote: [ -> ]Isn't Python marvellous, it gives us many different ways to kill a cat!

And a little like Schrödinger's cat; it can be alive or dead until script exit: the box is opened!
What you should do first is read the documentation for the function you are trying to use. Examples should supplement, not replace, reading the documentation.

https://docs.python.org/3/library/functions.html

Quote:print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Print objects to the text stream file, separated by sep and followed by end. sep, end, file, and flush, if present, must be given as keyword arguments.

Your example code could thus be written as:
color = str.lower(input ("Please enter your favorite color "))
print ("I don't like", color,", I prefer red", sep="")
Personally I prefer using f"string formatting for something like this. I think using + to concatenate strings is hideous, but it also an option. Even so it is still a good idea to know what print can do. For example, there are more than a few posts in this forum that ask "How can I print a list without the []? This can be done using print and sep.
numbers = list(range(1, 11))
print(numbers)
print(*numbers, sep=", ")
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
There are also posts asking "How can I do multiple prints on one line?" This can be done using print and end.
numbers = list(range(1, 5))
for n in numbers:
    print(n**2)
for n in numbers:
    print(n**2, end=" ")
Output:
1 4 9 16 1 4 9 16
You can also use print to write output to a file instead of stdout. There are more than a few posts asking "How do I write strings to a file with linefeeds?" Print automatically appends output with a linefeed unless configured to do otherwise.
with open("test.txt", "w") as file:
    for n in range(1, 5):
        print(n**2, file=file)
test.txt file contains:
Output:
1 4 9 16
File does contain a blank line at the end.