Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python how to reassign dupilcate string values to a new string

  • I take in a string aabullc

  • newstring should be assigned aall after the loop since a and l are duplicates

  • strings with more then 1 dupe like aaabullc should return aaall

    MEDevel.com: Open-source for Healthcare and Education

    Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

    Visit Medevel

    def rearrangeLetters(S):
      print(len(S))
      start = 0
      newstring = ""
    
      for i in range(start+1, len(S)):
          if(S[start] == S[i]):
              newstring+=S[i]
              print(newstring)
              start +=1
    

issue:

print(newstring) is never running

>Solution :

Try this:

def rearrangeLetters(S):
    s = [S[i] for i in range(len(S))]
    newstring = ""
    for t in s:
        if s.count(t) > 1:
            
            newstring += t
    return newstring

call function:

output = rearrangeLetters("aabullc")
print(output)

output:

aall
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading