Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Change uppercase letter

I can't figure out how to replace the second uppercase letter in a string in python.

for example:

string = "YannickMorin" 

I want it to become yannick-morin

As of now I can make it all lowercase by doing string.lower() but how to put a dash when it finds the second uppercase letter.

like image 831
Yannick Avatar asked Mar 14 '23 06:03

Yannick


1 Answers

You can use Regex

>>> import re
>>> split_res = re.findall('[A-Z][^A-Z]*', 'YannickMorin')
['Yannick', 'Morin' ]
>>>'-'.join(split_res).lower()
like image 182
Shankar Avatar answered Mar 24 '23 06:03

Shankar