Answer to Question #224714 in Python for ajay

Question #224714

Given a string in camel case, write a python program to convert the given string from camel case to snake case.

how to slove and what is wrong in my code



w = input() 

first_char = w[0]

first_char = first_char.lower()

#print(first_char)

remining_chars = w[1:] 

capl = 0

for char in remining_chars:

  if char == char.upper():

    break

  capl -= 1

   

lower = w[:capl].lower()

new = w[lower:] 

print(new)


1
Expert's answer
2021-08-09T16:25:41-0400
w = input('Enter string in camel case: ')
list1 = []


for i in w:
  if i.isupper() == True or w.index(i)==0:
    list1.append(i)
for i in w:
  if i in list1[1:]:
    print('_'+i.lower(),end='')
  elif i == list1[0]:
    print(i.lower(),end='')
  else:
    print(i,end='')

Enter string in camel case: headOfDepartment
head_of_department

#There is an error in the second to the last line of your code as you used the
#string for slice indices

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS