Python Forum

Full Version: How can i restrict the number of characters in an input?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, world!

So, i wanna restrict the number of characters in an input, for exemple:

num1 = int(input("Type your age: ")) #here i want the user can only type 3 or < characters
If I were going to do this, I'd check the length of the return value of input() (or the size of num1) and then re-prompt the user on rejection. If you are really serious about limiting the number of typed characters... I'm not as sure. I'd look at the curses module, or consider a GUI of some kind (likely a web server, like Flask, or Kivy since it would be easier to get working on Android). Other options would be platform-specific, which you might be fine with but I wouldn't be sure how to go about it.