Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How can i restrict the number of characters in an input?
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.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Find Average of User Input Defined number of Scores DustinKlent 1 217 Oct-25-2019, 12:40 AM
Last Post: Larz60+
  Restrict / Limit variable size mln4python 4 455 Aug-13-2019, 07:17 AM
Last Post: mln4python
  Split Column Text by Number of Characters cgoldstein 3 509 Mar-11-2019, 01:45 PM
Last Post: perfringo
  How to format characters that are given by user input with user input i1928 1 532 Feb-28-2019, 03:25 AM
Last Post: ichabod801
  Counting number of characters in a string Drone4four 1 858 Aug-16-2018, 02:33 PM
Last Post: ichabod801
  Regex: How to say 'any number of characters of any type until x'? JoeB 2 837 Jan-24-2018, 03:30 PM
Last Post: Mekire
  The number of occurrences of statistical characters RobertW 3 1,875 Jan-18-2018, 10:10 PM
Last Post: nilamo
  Number of characters ejstik 9 1,178 Jan-11-2018, 12:20 PM
Last Post: Larz60+
  Guess Random Number Why i m not able to enter input Nithya Thiyagarajan 6 3,715 Jan-07-2018, 04:26 AM
Last Post: squenson
  Beginner question: help ensuring input is a number ycrad 2 1,077 Dec-23-2017, 10:49 PM
Last Post: snippsat

Forum Jump:

Users browsing this thread: 1 Guest(s)