Skip to content

Commit 9b8e554

Browse files
Suryapratap Singh SuryavanshiSuryapratap Singh Suryavanshi
Suryapratap Singh Suryavanshi
authored and
Suryapratap Singh Suryavanshi
committed
Added is_kebab_case utility in string class
1 parent 03a4251 commit 9b8e554

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

strings/is_kebab_case.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
CheckKebabCase method checks the given string is in kebab-case or not.
3+
Problem Source & Explanation: https://en.wikipedia.org/wiki/Naming_convention_(programming)
4+
"""
5+
6+
import re
7+
8+
9+
def is_kebab_case(var_name: str) -> bool:
10+
"""
11+
CheckKebabCase method checks the given string is in kebab-case or not.
12+
Problem Source & Explanation: https://en.wikipedia.org/wiki/Naming_convention_(programming)
13+
>>> is_kebab_case('variable-name')
14+
True
15+
>>> is_kebab_case('VariableName')
16+
False
17+
>>> is_kebab_case('variable_name')
18+
False
19+
>>> is_kebab_case('variableName')
20+
False
21+
"""
22+
23+
if not isinstance(var_name, str):
24+
raise TypeError("Argument is not a string.")
25+
26+
pat = r"(\w+)-(\w)([\w-]*)"
27+
return bool(re.match(pat, var_name)) and "_" not in var_name
28+
29+
30+
if __name__ == "__main__":
31+
from doctest import testmod
32+
33+
testmod()
34+
input_var = input("Enter the variable name: ").strip()
35+
36+
status = is_kebab_case(input_var)
37+
print(f"{input_var} is {'in ' if status else 'not in '}kebab-case.")

0 commit comments

Comments
 (0)