TABLE 10.3 String-Testing Functions
The string-testing functions help when you need to validate input data that your scripts receive. If a
script requests a numeric value from the user, it’s a good idea to test what value the user enters before
actually using it in your code! You can try this out by creating a test script.
Try It Yourself: Test Strings
Try adding a string-testing feature to a small script by following these steps:
- Open your favorite text editor and add the following code:
Click here to view code image
#!/usr/bin/python3
choice = input('Please enter your age: ')
if (choice.isdigit()):
print('Your age is ', choice)
else:
print('Sorry, that is not a valid age')
- Save the file as script1001.py in your Python code folder.
- From the command prompt, run the program:
python3 sscript1001.py
The script1001.py script uses the isdigit() string function to test the string