Reputation: 33
I have written this little program to change camelCase function descriptions into snake_case descriptions.
def main():
#Get input and pass it to function
string = input("Camelcase: ")
make_snake(string)
def make_snake(string):
for char in string:
if char.isupper():
string = string.replace(char, "_"+ (char))
string_new = string.lower()
print(f"snake_case: {string_new}")
main()
It works fine with camelCase words with one capital letter like firstName, but when I run it with camelCases with two capital letters like "preferredFirstName" print returns two lines:
preferred_firstname preferred_first_name
I want it to return only the second line. Any idea why that is? Thank you so much for your time! Michael
Upvotes: 0
Views: 58
Reputation: 343
You need to wait till all letters have processed to print the converted string.
>>> def make_snake(string):
... for char in string:
... if char.isupper():
... string = string.replace(char, "_"+ (char))
... string_new = string.lower()
... print("snake_case: "+string_new)
...
>>> make_snake('preferredFirstName')
snake_case: preferred_first_name
>>>
Upvotes: 2