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

Manipulate string in for loop

I’m quite new to Python regarding string manipulation.
I’ve a field containing some object labels and want to insert text before or behind it.
For this I’m using values from a list to search the string for specific values.
Once I do this I’m not able to manipulate all found results, only for just one value.
I know that strings work differently in Python (immutable), but don’t know how to resolve this problem any other way.

objectList = [('Object A','Object B','Object C', 'Object D')]
objectString = '"Object A", "Object B", "Object C", "Object D"'

def transform_toString(objects, old_format):
    obj = objects
    new_format = old_format
    for o in obj:
        position = new_format.find(str(o))
        new_format = new_format[:position] + ' found' + new_format[position:]
    return new_format
    
transformed_string = transform_toString(objectList, objectString)

This results in the following output:

"Object A", "Object B", "Object C", "Object D found"

How can I achieve the following output?

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

"Object A found", "Object B found", "Object C found", "Object D found"

>Solution :

There are multiple errors in your code.

  1. I would recommend you to install an IDE with debugger, so that you can jump in the forloop and see what is happening there.

  2. If you look a bit closer, you can see that you are iterating an array of 1 element and that is ('Object A','Object B','Object C', 'Object D') (it is a tuple of 4 items) and you are searching for the whole tuple in the string. This would normally result in an error in different programming languages, however here it tries to find the whole tuple

  3. And the reason why it writes the found at the end is that if you look at the return value (I recommend checking out documentation as well) iit returns the -1 which means the element was not found. In python, -1 indicates the last element in the array (in this case last character in the string)

However, if you fix the type, the result still not be the desired one. If you remove the parantases, you will get " foundObject A", " foundObject B", " foundObject C", " foundObject D". In order to fix this, you need to move your pointer (position) by the offset of the word length.

The fixed code:

objectList = ['Object A', 'Object B', 'Object C', 'Object D']
objectString = '"Object A", "Object B", "Object C", "Object D"'


def transform_toString(objects, old_format):
    obj = objects
    new_format = old_format
    for o in obj:
        position = new_format.find(str(o))
        if position == -1:
            continue
        position += len(o)
        new_format = new_format[:position] + ' found' + new_format[position:]
    return new_format


transformed_string = transform_toString(objectList, objectString)
print(transformed_string)
#  "Object A found", "Object B found", "Object C found", "Object D found"

Additionally, I recommend you to first try to debug your code and then ask a question. This seems like a simple task given to you at school.

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