Replacing part of a string in a list in python
<p>For example i have list of a name written diffently  </p>

<pre><code>list1 = ["jai.kumar","jaikumar","j_kumar","jk","kumar-jai","ja.ku"]
for str in l1:
 if str == âjaiâ
  str.replace (âjaiâ,âfirstnameâ)
   if str == âjaâ
    str.replace (âjaâ,âfirst 2 character of firstnameâ)
     if str == âjâ
      str.replace (âjâ,âfirst character of firstnameâ)
 if str == âkumarâ
  str.replace (âkumarâ,âlastnameâ)
   if str == âkuâ
    str.replace (âkuâ,âfirst 2 character of lasttnameâ)
     if str == âkâ
      str.replace (âkâ,âfirst character of lastnameâ)
print(list1)
</code></pre>

<p>How do i correct the above code or is there an easier way to do it?</p>

<p>My expected output is</p>

<pre><code> list1 = ["firstname.lastname","firstnamelastname","first character of firstname_lastname",.....]
</code></pre>

<p>I assume you meant sth like this?:</p>

<pre><code>if str == âjaiâ:
    str.replace (âjaiâ,âfirstnameâ)
    if str == âjaâ:
        str.replace (âjaâ,âfirst 2 character of firstnameâ)
        if str == âjâ:
            str.replace (âjâ,âfirst character of firstnameâ)
</code></pre>

<p>If yes(remeber of semicolon at the end of 'if') then this code won't work as you wish because second and third if is nestled inside previous IFs.
So if first condition -> str=="jai" fails then you don't check the next ones.
It should be:</p>

<pre><code>if str == âjaiâ:
    str.replace (âjaiâ,âfirstnameâ)
elif str == âjaâ:
    str.replace (âjaâ,âfirst 2 character of firstnameâ)
elif str == âjâ:
    str.replace (âjâ,âfirst character of firstnameâ)
</code></pre>

python