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>You could use a <a href="https://docs.python.org/3.5/library/re.html">regular expression</a> with a positive lookbehind assertion:</p>

<pre><code>&gt;&gt;&gt; import re
&gt;&gt;&gt; s  ="UserID/ContactNumber"
&gt;&gt;&gt; re.sub('(?&lt;=[a-z])([A-Z])', r'-\1', s).lower()
'user-id/contact-number'
</code></pre>

python python-2.7 python-3.x