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
12 Antworten
+ 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)
+ 2
No problem 👍👍
+ 1
codemonkey wow that was smart thank you
+ 1
codemonkey I need to change it so that mine also iterate through a list to avoid such errors
Thank you so much 🙏
0
codemonkey what was the problem? It worked fine though
0
codemonkey ohh now I see mine has the same problem 😅
0
Abhay yess you are right thank you so much
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
0
Shubhank Gupta this is nice code than you so much 🙏
0
pedram ch you can put [Solved] tag in the title of the Discussion
0
Erialdo Meta 🇦🇱 yesss re is good idea in this case thankss