pythonstringlowercasecapitalizationmixed-case

How to check if a random mixed case word is in a string


I am trying to check to see if there is the word robot in a line. (I'm learning python. Still very new to it.) If the word 'robot' is in a line it prints something out. Same with 'ROBOT'. However, I need to know how to output when robot is in the line but in a randomly mixed case, eg rObOt. Is this possible? It seems like I would need to write out every combination. I'm using Python 3. Thanks :).

if ' robot ' in line:
  print("There is a small robot in the line.")
elif ' ROBOT ' in line:
  print("There is a big robot in the line.")
elif 'rOBOt' in line:
  print("There is a medium sized robot in the line.")
else:
  print("No robots here.")

Solution

  • Hope the code below can help you.

    line = "Hello robot RoBot ROBOT"
    
    l = line.split(" ")
    
    exist = False
    
    for word in l:
        if word.upper() == "ROBOT":
    
            exist = True
    
            if word.isupper():
                print("There is a big robot in the line.")
            elif word.islower():
                print("There is a small robot in the line.")
            else:
                print("There is a medium sized robot in the line.")
    
    if not exist:
        print("No robots here.")