Printing String after the user input on the same line
Question:
so basically i want to use the python input() function to get a velocity input from the user. After the user types the number i want to display a nit like m/s behind the user input.
I thought i can do something like this:
velocity = input("Please enter a velocity: ", end='')
print("m/s")
but end=” only works with print() and not with input(). Do you have any other ideas?
Thank you in advance.
Answers:
You are already reading the user input into a variable called velocity
. So you already have the value, you just need to print it and append m/s
to it.
Here’s a way to do it –
velocity = input("Please enter a velocity: ")
print(velocity, end="m/sn")
n
is newline character and is adding a newline after m/s
Here’s a test run –
~ python my_test.py
Please enter a velocity: 67
67m/s
For printing you can use different versions –
print(f"{velocity}m/s")
print("{}m/s".format(velocity))
Based on comments, the OP wants the input field itself to be updated with that value. This behaviour is not supported by the input()
function by default, and would involve terminal text manipulation.
You can use curses to manipulate the strings.
If you only want to update the previous line once the user has given input, you can use the ANSI escape code for going to previous line(
so basically i want to use the python input() function to get a velocity input from the user. After the user types the number i want to display a nit like m/s behind the user input.
I thought i can do something like this:
velocity = input("Please enter a velocity: ", end='')
print("m/s")
but end=” only works with print() and not with input(). Do you have any other ideas?
Thank you in advance.
You are already reading the user input into a variable called velocity
. So you already have the value, you just need to print it and append m/s
to it.
Here’s a way to do it –
velocity = input("Please enter a velocity: ")
print(velocity, end="m/sn")
n
is newline character and is adding a newline after m/s
Here’s a test run –
~ python my_test.py
Please enter a velocity: 67
67m/s
For printing you can use different versions –
print(f"{velocity}m/s")
print("{}m/s".format(velocity))
Based on comments, the OP wants the input field itself to be updated with that value. This behaviour is not supported by the input()
function by default, and would involve terminal text manipulation.
You can use curses to manipulate the strings.
If you only want to update the previous line once the user has given input, you can use the ANSI escape code for going to previous line(