25. Other string things and IF statements

  • All the different ways of checking strings and characters on the last page return either TRUE (1) or FALSE (0). This means that they can be used with IF statements
  • If you played around with the input string, you should have realised that islower() and isupper() only return true when the entire string is a particular case
#Ask for a character
character = input("Enter a character: ")

#Check for a password length
if len(character) != 1:
    print("Only one character should be entered")

#Check if password is lowercase
if character.islower() == 1:
    print("Character is lowercase")

Tasks

  1. Type in the code above and test it works
  2. Modify the program to print out a message to say if the character is uppercase or lowercase
  3. Modify the program to print a message if the character is a number
  4. Save and label your program

Hints

  • You can use these techniques to perform rigorous checks on user input to protect your program
  • Checking user input is called validation and it helps make sure your program runs as expected