0

Code:

s = input("Enter a String : \n")
print("The Entered String is : " + s)
print("The Length of Entered String is : " + str(len(s)))

Output:

┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $python try.py
Enter a String : 
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $python try.py
Enter a String : 
hello^[[D
The Entered String is : hello
The Length of Entered String is : 8

When I press the arrow keys ^[[C show up instead of the cursor moving (similar thing happens with other arrow keys, escape key, home, end)!

Whats happening here is the second time the string has the characters :

['h', 'e', 'l', 'l', 'o', '\x1b', '[', 'C']

So, the '\x1b', '[', 'C' is the sequence of characters send to the shell from the keyboard for representing right arrow key(cursor forward).

What i want is that these characters will not show up in the shell but the cursor will move (forward, backward, to home, end, etc as per the key pressed).

Processing after taking the input is meaning less as the main aim is to let the cursor be moved!

How can i achieve this in Python?

Exception

But its ok if we use the python interpreter directly :

Here is the Terminal output :

┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $python
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> s = input("Enter a String : \n")
Enter a String : 
hello
>>> print("The Entered String is : " + s)
The Entered String is : hello
>>> print("The Length of Entered String is : " + str(len(s)))
The Length of Entered String is : 5
>>> s = input("Enter a String : \n")
Enter a String : 
hello
>>> print("The Entered String is : " + s)
The Entered String is : hello
>>> print("The Length of Entered String is : " + str(len(s)))
The Length of Entered String is : 5

Here in spite of pressing the arrow keys or escape or home the output is same.

[EDIT]

The one and only aim is to give the user a exact terminal like experience while using the program.

Jaysmito Mukherjee
  • 1,467
  • 2
  • 10
  • 29

1 Answers1

0

The documentation for input mentions the following:

If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.

So, we take your program, and add an import readline, and now we get cursor handling and other items from it:

import readline

s = None

while s != 'exit':
    s = input("Enter a String : \n")
    print("The Entered String is : '{}'".format(s))
    print("The Length of Entered String is : {}".format(len(s)))

in this case I can do a run of it:

Enter a String :
hello world
The Entered String is : 'hello world'
The Length of Entered String is : 11
Enter a String :
hello   world
The Entered String is : 'hello   world'
The Length of Entered String is : 13
Enter a String :
exit
The Entered String is : 'exit'
The Length of Entered String is : 4

I was able to use the up arrow to go back and edit the previous input.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122