Single character input? | Sololearn: Learn to code for FREE!

+1

Single character input?

How to make single character input in python?like getch() is there any function in python

4/13/2020 1:32:23 PM

Lalis

7 Answers

New Answer

+5

If you just want to work without using Return-key, there seems to be no easy way. But you can check this post, there are several approaches explained: https://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user

+4

Type only one char in the input. It will still be taken as string. ch = ord(input()[0]) will gjve you ascii value of the first char. To convert back print(chr(ch)) # convert the ascii value to char but still represented as string

+2

rodwynnejones I realized there is no point in doing that since the value stored is still of type "string". Better type only single char(string)

+2

swim....ah...didn't notice you already had the [0] in you original response..forgive me. I just realised what Lilas was looking for....(although a python version) When using getch() from the conio header file..there is no need to hit "enter" after entering a character...but.......my code snippet doesn't do that.....sorry Lilas.

+2

thanks for help But i have to press enter after that.any other solution plz

+1

Wasn't sure if this would work...but... had a go and it does actually work.. myinput = input()[0] print(myinput)

+1

https://www.sololearn.com/Discuss/2235815/?ref=app