Issue
Here is my code to replace a camelCase input with snake_case:
camel_case = str(input(" "))
snake_case = ""
# check each characters in string
for char in camel_case:
if char.isupper(): # checking if it is upper or not
snake_case = camel_case.replace(char, "_"+char.lower())
print(snake_case)
And with the input userName
, it outputs user_name
. But with the input with more than two Capital characters, for example, goodUserName
only outputs goodUser_name
.
Solution
Don't modify the string in place. Instead, just create a new string:
output = ""
for char in camel_case:
if char.isupper():
output += "_"
output += char.lower()
Or, in one line:
"".join("_"+char.lower() if char.isupper() else char.lower() for char in camel_case)
Answered By - not_speshal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.