I'm working on the MIT intro to computation and programming course and I'm trying to store a multi-line string in a variable that I can use for the program to interact with the user.
I know about the """
for inputting long lines of code with carriage return inserting a newline character (I think I phrased that somewhat accurately).
What I'm running into is the string being stored look shitty in my code and it looks a lot cleaner to use the triple quote but I still want it to print out on one line. I am trying to store it in a variable like so:
inputRequest = """
Enter 'h' to indicate the guess is too high.
Enter 'l' to indicate the guess is too low.
Enter 'c' to indicate the guess is correct.
"""
and I tried to call that variable in my console like this:
print(inputRequest, end=" ")
but it still prints out on three separate lines. Is there an efficient way to do this so that my code doesn't look messy? Storing the string in a variable seems like a good way to cut down on my typing when I need to call that particular output for the user to interact with, but I'm sure there are better ways to do this.
You can place backslashes at the end of each line to prevent the newline character from printing in your strings.
inputRequest = """\
Enter 'h' to indicate the guess is too high. \
Enter 'l' to indicate the guess is too low. \
Enter 'c' to indicate the guess is correct. \
"""
print(inputRequest)
If you want, you can also use separate strings for the same purpose.
inputRequest = \
"Enter 'h' to indicate the guess is too high. " \
"Enter 'l' to indicate the guess is too low. " \
"Enter 'c' to indicate the guess is correct. " \
print(inputRequest)