3

This may seem a bit silly or obvious to a lot of you, but how can I print a string after entering an input on the same line?

What I want to do is ask the user a question then they enter their input. After they press enter I want to print a selection of text, but on the same line after their input, instead of the next.

At the moment I am doing something the following for regular input/output:

Example = input()
print("| %s | Table1 | Table2 | Table3 |" % (Example))

Which outputs:

INPUT
| INPUT | Table1 | Table2 | Table3 |

However, what I would like to get is just:

| INPUT | Table1 | Table2 | Table3 |

Thank you for your time.

CStock5
  • 33
  • 1
  • 1
  • 5

4 Answers4

1

From what I understood you want the input of the user to be replaced by the output of the program. So what you would need would be to delete some characters before printing. I think that this post here contains the answer you want:

How to overwrite the previous print to stdout in python?

Edit: From the comment, maybe you can use this solution instead, it seems "harsh" but could do the job :

remove last STDOUT line in Python

Community
  • 1
  • 1
bserra
  • 520
  • 4
  • 13
  • This was really helpful and was closer to what I was after, but I can't seem to get it delete the original output from the input() command. I think this may be because the after the input(), it calls a newline. ­ Do you have any suggestions how I could go back, or even delete the line after pressing the enter key? – CStock5 Aug 01 '13 at 18:02
1

If you want to keep the screen empty, and control what appears each time the user puts in user input, you can clear the screen very easily, and then print immediately after

import os
os.system("cls") #if you're on windows, for linux use "clear"

Here is an example

Example = input()
os.system("cls")
print("| %s | Table1 | Table2 | Table3 |" % (Example))
Stephan
  • 16,509
  • 7
  • 35
  • 61
1

Use the end parameter, so after you type in the text in the input function, put an end=" " after. Like this:

                           b=input("Hi a random example.", end=" ")
                           print("This is on the same line.")

It will then print on the same but it must only be the parameter end and nothing else.

  • This was actually the first thing I tried to do when I wanted to achieve my goal. I found the input() function has no arguments to do anything similar (yes, I looked at the documentation before asking this question). As such, entering what you have just put is not valid. My issue is deleting the original input and the new-line it creates or adding the text onto the end. – CStock5 Aug 01 '13 at 21:31
0

Have you looked at getpass? It reads an input without displaying the characters the user types (like for a password). The first argument to the method is the prompt to display to the user, which it looks like you want to be "" (the empty string). Try this:

from getpass import getpass
Example = getpass("")

print("| %s | Table1 | Table2 | Table3 |" % (Example))

And you should get your desired output. Good luck!

Edit

Although, you may still end up with a blank line where the user types their input. This may or not may not fit the requirements you have, but I think it's a step closer than what you have in the original post.

c.maclean
  • 401
  • 2
  • 9