'I have a space after a return string in python that I don't know how to get rid of or where it's coming from
My code below is just a fun, small Mad Lib Generator to help start learning how to program in python on vscode. I am using python3 I believe I don't know if that makes a difference, however, when I print the string part1 there is a space between c_noun1 and "-footed" that I don't want and don't know how to get rid of that space. I feel like there is a simple way to do it. I'm thinking of using split() and taking out the space that way, but I think that's too much. Can someone please help me get rid of the space separating the c_noun1 and "-footed". I really want the statement to print "eight-footed" instead of eight -footed.
from operator import truediv
def colored(r, g, b, text):
return "\033[38;2;{};{};{}m{} \033[38;2;255;255;255m".format(r, g, b, text)
def word(string):
retry = True
while (retry):
print(string)
a = input()
if(a.isnumeric()):
print("Please do not enter a number!")
retry = True
else:
retry = False
return a
opening = "This is a Mad Lib Generator!"
Title = colored(255, 0, 0, opening)
print(Title)
#Stores users inputs
noun1 = word("Please enter a noun")
pnoun1 = word("Please enter a noun(plural)")
noun2 = word("Please enter a noun")
pnoun2 = word("Please enter a noun(plural)")
noun3 = word("Please enter a place")
adj = word("Please enter a adjective")
noun4 = word("Please enter a noun")
#colouring words
c_noun1 = colored(0, 255, 0, noun1)
c_pnoun1 = colored(0, 255, 0, pnoun1)
c_noun2 = colored(0, 255, 0, noun2)
c_pnoun2 = colored(0, 255, 0, pnoun2)
c_noun3 = colored(0, 255, 0, noun3)
c_adj = colored(0, 255, 0, adj)
c_noun4 = colored(0, 255, 0, noun4)
# #sentences with the string variables
part1 = "Be kind to your " + c_noun1 + "-footed" + c_pnoun1
part2 = "For a duck my be somebody's " + c_noun2 + ","
part3 = "Be kind to your " + c_pnoun2 + "in " + c_noun3
part4 = "Where the weather is always " + c_adj
part5 = "You may think that this is the " + c_noun4
part6 = "Well it is."
#print sentences
print(part1.lower())
print(part2.lower())
print(part3.lower())
print(part4.lower())
print(part5.lower())
print(part6.lower())
Solution 1:[1]
Try removing the space between the } and \033 in the colored function, like this:
def colored(r, g, b, text):
return "\033[38;2;{};{};{}m{}\033[38;2;255;255;255m".format(r, g, b, text)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jorge Morgado |
