Search code examples
pythonuppercasealphabetcaesar-cipher

How to load an uppercase alphabet?


I have been trying to create a Caesar cipher program. To make it as inclusive as possible I want to be able to use upper case letters as well. I know how to load a lower case alphabet:

alphabet = string.ascii_lowercase * 2

(I have timed it by two to allow the user to encrypt all letters) I would really like some help. It is my first time submitting a question and I would appreciate any help I get


Solution

  • There is string.ascii_uppercase as well as string.ascii_lowercase (documentation). For testing if a letter is upper case and do something for uppercase letters in a message you can do this:

    for letter in message:
        if letter in string.ascii_uppercase:
            # do something
    

    You can also use isupper() method (documentation):

    for letter in message:
        if letter.isupper():
            # do something
    

    If you want to check whether the whole message is upper case, then isupper() is actually better as it checks the whole string:

    if message.isupper():
        # do something