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
#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


Messages In This Thread
RE: Remove a space between a string and variable in print - by deanhystad - Jul-27-2022, 02:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  remove gilberishs from a "string" kucingkembar 2 365 Mar-15-2024, 08:51 AM
Last Post: kucingkembar
  Replacing String Variable with a new String Name kevv11 2 875 Jul-29-2023, 12:03 PM
Last Post: snippsat
  Print variable without '' and spaces arnonim 1 778 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 2,269 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  Problem with print variable in print.cell (fpdf) muconi 0 711 Dec-25-2022, 02:24 PM
Last Post: muconi
  python r string for variable mg24 3 3,121 Oct-28-2022, 04:19 AM
Last Post: deanhystad
Smile please help me remove error for string.strip() jamie_01 3 1,297 Oct-14-2022, 07:48 AM
Last Post: Pedroski55
  USE string data as a variable NAME rokorps 1 1,038 Sep-30-2022, 01:08 PM
Last Post: deanhystad
  Removing Space between variable and string in Python coder_sw99 6 6,518 Aug-23-2022, 01:15 PM
Last Post: louries
  Split string using variable found in a list japo85 2 1,389 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