Python Forum
How can i restrict the number of characters in an input?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can i restrict the number of characters in an input?
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  restrict user input to numerical values MCL169 2 869 Apr-08-2023, 05:40 PM
Last Post: MCL169
  Find if chain of characters or number Frankduc 4 1,752 Feb-11-2022, 01:55 PM
Last Post: Frankduc
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 3,725 Oct-19-2020, 04:16 AM
Last Post: deanhystad
  if the input is not number, let user input again teatea 14 8,152 Jun-02-2020, 07:03 PM
Last Post: ibutun
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,416 May-15-2020, 01:37 PM
Last Post: snippsat
  Get input directly as a number? Pedroski55 4 2,152 May-05-2020, 04:29 PM
Last Post: deanhystad
  Find Average of User Input Defined number of Scores DustinKlent 1 4,202 Oct-25-2019, 12:40 AM
Last Post: Larz60+
  Restrict / Limit variable size mln4python 4 7,080 Aug-13-2019, 07:17 AM
Last Post: mln4python
  Split Column Text by Number of Characters cgoldstein 3 2,948 Mar-11-2019, 01:45 PM
Last Post: perfringo
  How to format characters that are given by user input with user input i1928 1 2,395 Feb-28-2019, 03:25 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020