1

I am using raw_input("> ") to write something. And after that I am using len() to get the character count.

something = raw_input("> ")
print something
print len(something)

Is it possible to get the character count while writing at the beginning or the end of the text I write? So at the moment when I write A test (6 characters) I would like to see "6" in real time (e.g. at the beginning before the "> " How can I do this?

Johnny
  • 173
  • 1
  • 3
  • 12

1 Answers1

1

Python is in line buffered mode by default, thus raw_input does not return until you press the enter key. If you want to read a single char immediately, see Python read a single character from the user.

Then you can do it this way though the need is rather useless I think:

getch = _Getch() #_Getch is defined in the post I mentioned above
a=''
cnt=0
inputs=''
while True:
    print '\r%6d> '%cnt, inputs, 
    a=getch()
    if ord(a) == 13:
        break
    cnt+=1
    inputs+=a

print '\nuser inputs:', inputs
Community
  • 1
  • 1
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108