Radeen10-_

Convert String to Camel Case

Jul 28th, 2021 (edited)
179
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #'the-stealth-warrior' gets converted to "theStealthWarrior"
  2. #'the_stealth_warrior' gets converted to "theStealthWarrior"
  3. def camel_to_case(text):
  4.     #a list for initially store data
  5.     txt=[]
  6.     #create an empty string to store the final list arrays
  7.     camel_text=''
  8.     start=0
  9.     for i, pointer in enumerate(text):
  10.         if pointer=='_' or pointer=='-':
  11.             txt.append(text[start:i])
  12.             start=i+1
  13.     if len(text)>start:
  14.         txt.append(text[start:])
  15.       for i, txt in enumerate(txt):
  16.         if i == 0: #first character remain same as the input
  17.             camel_text += txt
  18.         else:
  19.             camel_text+=txt.capitalize()
  20.     return camel_text
  21.  
  22. print(camel_to_case(input(""))
  23. # input as ( To-Test_prb_math-phy_botany-_start)
  24. # it returns (ToTestPrbMathPhyBotanyStart)
  25.  
  26.  
RAW Paste Data