0

I am new to coding. And I would like to know if there's a way for input function to not print newline character after the value is entered. Something like print function's argument end. Is there any way?

  • Can you please give more information about your example? – berkayln Apr 16 '21 at 18:23
  • 2
    Take a look [here](https://stackoverflow.com/questions/7173850/possible-to-get-user-input-without-inserting-a-new-line). But the answer for the most part is no. – Chrispresso Apr 16 '21 at 18:24

2 Answers2

1

Well, you can't make input() trigger by anything besides 'Enter' hit (other way may be using sys.stdin and retrieving character one-by-one until you receive some stop marker, but it's difficult both for programmer and for user, I suppose). As a workaround I can the suggest the following: if you can know the length of line written before + length of user input, then you can use some system codes to move cursor back to the end of previous line, discarding the printed newline:

print("This is first line.")
prompt = "Enter second: "
ans = input(prompt)
print(f"\033[A\033[{len(prompt)+len(ans)}C And the third.")

\033[A moves cursor one line up and \033[<N>C moves cursor N symbols right. The example code produces the following output:

This is first line.
Enter second: USER INPUT HERE And the third.

Also note that the newline character is not printed by your program, it's entered by user.

STerliakov
  • 4,983
  • 3
  • 15
  • 37
-1

name=input('Enter your name : ') print('hello',name,end='') I know about the end function which is abov