How do I limit the amount of letters in a string
25,934
Solution 1
Python's input
function cannot do this directly; but you can truncate the returned string, or repeat until the result is short enough.
# method 1
answer = input("What's up, doc? ")[:10] # no more than 10 characters
# method 2
while True:
answer = input("What's up, doc? ")
if len(answer) <= 10:
break
else:
print("Too much info - keep it shorter!")
If that's not what you're asking, you need to make your question more specific.
Solution 2
You can get only the first n characters of the input text like so:
data = raw_input()[:10]
Author by
Jacob cummins
Updated on August 03, 2022Comments
-
Jacob cummins almost 2 years
I have a program that asks a the user to input a question, the program then answers it. What I want to know is how do I limit the amount of letters a user can input into a variable.