pythonstringcapslock

How to reverse caps lock in a string in python?


I have a string that can either be written with caps lock or not. "With caps lock" means that it is either is like tHIS or like THIS. It is easy enough to detect the second case with "isupper()" function, but I wasn't able to find a way to find the first case reliably. For strings of length 1 I used "islower()" to detect if they should be capitalized, so it shouldn't be a problem

Code I used

import re
inp = input()
trutable = ""
for i in inp:
    if i.isupper():
        trutable += "1"
    if i.islower():
        trutable += "0"
pattern = re.compile(r'^01')
answ = re.match(pattern, trutable)
if inp.isupper() or answ != None or (len(inp) == 1 and inp.islower()):
    inp = inp.capitalize()

print(inp)

Solution

  • For text character replacement python string has the str.maketrans and str.translate methods:

    from string import ascii_lowercase as ascii_up, ascii_uppercase as ascii_low
    
    def reverseCase(text):
        m = str.maketrans(ascii_low + ascii_up, ascii_up + ascii_low)
        return text.translate(m)
    
    
    for w in ("tata", "TATA", "TaTa", "42"):
        print(reverseCase(w))
    

    Output:

    TATA
    tata
    tAtA
    42
    

    To detect things

    [...] either is like 'tHIS' [...]

    you can use:

    def isWordStartingLowerAndContinuingUpperCased(word):
        """Check if words starts with lower case and continues upper cased."""
        return word[0].islower() and word[1:].isupper()
    
    for w in ("tHIS", "This", "THIS"):
        print(w, ":", isWordStartingLowerAndContinuingUpperCased(w))
    

    To get

    tHIS : True 
    This : False
    THIS : False
    

    When normalizing text be aware of false positives - there is a huge group of words that belong into all-caps and should not be changed - abbreviations:

    NBL, USA, MIB, CIA, FBI, NSA, MI6, NASA, DARPA, etc.