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)
For text character replacement python string has the str.maketrans and str.translate methods:
Output:
To detect things
you can use:
To get
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.