Python Forum
Remove a space between a string and variable in print
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove a space between a string and variable in print
#1
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
Reply
#2
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.
Pedroski55, rob101, sie like this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(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.
sie and buran like this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#4
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: \'
sie and rob101 like this post
Reply
#5
(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!
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  remove gilberishs from a "string" kucingkembar 2 202 Mar-15-2024, 08:51 AM
Last Post: kucingkembar
  Replacing String Variable with a new String Name kevv11 2 729 Jul-29-2023, 12:03 PM
Last Post: snippsat
  Print variable without '' and spaces arnonim 1 694 Jan-30-2023, 05:23 PM
Last Post: deanhystad
  Need help on how to include single quotes on data of variable string hani_hms 5 1,883 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  Problem with print variable in print.cell (fpdf) muconi 0 632 Dec-25-2022, 02:24 PM
Last Post: muconi
  python r string for variable mg24 3 2,634 Oct-28-2022, 04:19 AM
Last Post: deanhystad
Smile please help me remove error for string.strip() jamie_01 3 1,149 Oct-14-2022, 07:48 AM
Last Post: Pedroski55
  USE string data as a variable NAME rokorps 1 918 Sep-30-2022, 01:08 PM
Last Post: deanhystad
  Removing Space between variable and string in Python coder_sw99 6 6,123 Aug-23-2022, 01:15 PM
Last Post: louries
  Split string using variable found in a list japo85 2 1,235 Jul-11-2022, 08:52 AM
Last Post: japo85

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020