mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-30 06:03:42 +00:00
Added is_kebab_case utility in string class
This commit is contained in:
parent
03a42510b0
commit
9b8e554c7d
37
strings/is_kebab_case.py
Normal file
37
strings/is_kebab_case.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
"""
|
||||
CheckKebabCase method checks the given string is in kebab-case or not.
|
||||
Problem Source & Explanation: https://en.wikipedia.org/wiki/Naming_convention_(programming)
|
||||
"""
|
||||
|
||||
import re
|
||||
|
||||
|
||||
def is_kebab_case(var_name: str) -> bool:
|
||||
"""
|
||||
CheckKebabCase method checks the given string is in kebab-case or not.
|
||||
Problem Source & Explanation: https://en.wikipedia.org/wiki/Naming_convention_(programming)
|
||||
>>> is_kebab_case('variable-name')
|
||||
True
|
||||
>>> is_kebab_case('VariableName')
|
||||
False
|
||||
>>> is_kebab_case('variable_name')
|
||||
False
|
||||
>>> is_kebab_case('variableName')
|
||||
False
|
||||
"""
|
||||
|
||||
if not isinstance(var_name, str):
|
||||
raise TypeError("Argument is not a string.")
|
||||
|
||||
pat = r"(\w+)-(\w)([\w-]*)"
|
||||
return bool(re.match(pat, var_name)) and "_" not in var_name
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from doctest import testmod
|
||||
|
||||
testmod()
|
||||
input_var = input("Enter the variable name: ").strip()
|
||||
|
||||
status = is_kebab_case(input_var)
|
||||
print(f"{input_var} is {'in ' if status else 'not in '}kebab-case.")
|
Loading…
Reference in New Issue
Block a user