CStock5
CStock5

Reputation: 33

How can I print a string on the same line as the input() function?

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.

Upvotes: 3

Views: 10476

Answers (4)

bserra
bserra

Reputation: 540

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

Upvotes: 1

user1800989
user1800989

Reputation:

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.

Upvotes: 1

Stephan
Stephan

Reputation: 18041

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

Upvotes: 1

c.maclean
c.maclean

Reputation: 411

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.

Upvotes: 0

Related Questions