Remove a space between a string and variable in print - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Remove a space between a string and variable in print (/thread-37826.html) |
Remove a space between a string and variable in print - sie - Jul-27-2022 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: 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 RE: Remove a space between a string and variable in print - menator01 - Jul-27-2022 Here is one way import re string = "I don't like purple , I prefer red." string = re.sub('[\s],', ',', string) print(string)
RE: Remove a space between a string and variable in print - rob101 - Jul-27-2022 (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. RE: Remove a space between a string and variable in print - Pedroski55 - Jul-27-2022 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: \' RE: Remove a space between a string and variable in print - rob101 - Jul-27-2022 (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! RE: Remove a space between a string and variable in print - deanhystad - Jul-27-2022 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) 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=", ") 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=" ") 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: File does contain a blank line at the end.
|