Check For Camel Case In Python


Answer :

You could check if a string has both upper and lowercase.

def is_camel_case(s):     return s != s.lower() and s != s.upper() and "_" not in s   tests = [     "camel",     "camelCase",     "CamelCase",     "CAMELCASE",     "camelcase",     "Camelcase",     "Case",     "camel_case", ]  for test in tests:     print(test, is_camel_case(test)) 

Output:

camel False camelCase True CamelCase True CAMELCASE False camelcase False Camelcase True Case True camel_case False 

Convert your string to camel case using a library like inflection. If it doesn't change, it must've already been camel case.

from inflection import camelize  def is_camel_case(s):     # return True for both 'CamelCase' and 'camelCase'     return camelize(s) == s or camelize(s, False) == s 

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?