Is there an easier way?[SOLVED] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is there an easier way?[SOLVED]

Hi all I wrote this code to solve a code coach problem The problem is to put "_" befor every capital letter(except first letter)and also make it all lowercase So "ThisIsAThing" would be "this_is_a_thing" I wanted to know if there is an easier way to do it since I feel 14 lines of code is alot for this. I hope this kind of questions are ok to ask since when I solve code coache problems I really like to know best ways to solve them and learn from them https://code.sololearn.com/c1RQyMTEd68M/?ref=app

13th Oct 2020, 5:10 PM
pedram ch
pedram ch - avatar
12 Answers
+ 4
You can do it this way as well a=input() if a[0].isupper(): a=a[0].lower()+a[1:] for i in a: if i.isupper(): b="_"+i.lower() a=b.join(a.split(i)) print(a)
13th Oct 2020, 6:28 PM
Abhay
Abhay - avatar
14th Oct 2020, 6:32 PM
Shubhank Gupta
Shubhank Gupta - avatar
+ 2
No problem 👍👍
15th Oct 2020, 9:39 AM
Shubhank Gupta
Shubhank Gupta - avatar
+ 1
codemonkey wow that was smart thank you
13th Oct 2020, 6:07 PM
pedram ch
pedram ch - avatar
+ 1
codemonkey I need to change it so that mine also iterate through a list to avoid such errors Thank you so much 🙏
13th Oct 2020, 6:34 PM
pedram ch
pedram ch - avatar
0
codemonkey what was the problem? It worked fine though
13th Oct 2020, 6:21 PM
pedram ch
pedram ch - avatar
0
codemonkey ohh now I see mine has the same problem 😅
13th Oct 2020, 6:27 PM
pedram ch
pedram ch - avatar
0
Abhay yess you are right thank you so much
13th Oct 2020, 6:33 PM
pedram ch
pedram ch - avatar
0
codemonkey it does work ,and it's actually more easy to understand for me ,you split the sentence from where capital word is found ,use "_"+capitalWord.lower().join() method to the join the list again
13th Oct 2020, 6:50 PM
Abhay
Abhay - avatar
0
Shubhank Gupta this is nice code than you so much 🙏
15th Oct 2020, 9:38 AM
pedram ch
pedram ch - avatar
0
pedram ch you can put [Solved] tag in the title of the Discussion
15th Oct 2020, 10:18 AM
Shubhank Gupta
Shubhank Gupta - avatar
0
Erialdo Meta 🇦🇱 yesss re is good idea in this case thankss
16th Oct 2020, 1:26 PM
pedram ch
pedram ch - avatar