How to convert the following string in python?
<p>Input : UserID/ContactNumber </p>

<p>Output: user-id/contact-number</p>

<p>I have tried the following code:</p>

<pre><code>s ="UserID/ContactNumber"

list = [x for x in s]

for char in list:

     if char != list[0] and char.isupper():
            list[list.index(char)] = '-' + char

 fin_list=''.join((list))
 print(fin_list.lower())
</code></pre>

<p>but the output i got is:</p>

<pre><code>  user-i-d/-contact-number
</code></pre>

<p>What about something like that:</p>

<pre><code>s ="UserID/ContactNumber"
so = ''
for counter, char in enumerate(s):
    if counter == 0:
        so = char
    elif char.isupper() and not (s[counter - 1].isupper() or s[counter - 1] == "/"):
        so += '-' + char
    else:
        so += char
print(so.lower())
</code></pre>

<p>What did I change from your snippet?</p>

<ul>
<li>You were checking if this is the first char and if it is a upper char.</li>
<li><p>I have add a check on the previous char, to not consider when the previous is upper (for the D of ID) or when the previous is \ for the C of Contact.</p></li>
<li><p>You are also editing the list <code>list</code> while iterating on it's element, that is dangerous.</p></li>
<li>I have instead create an output string to store the new values</li>
</ul>

python python-2.7 python-3.x