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

Removing single quotes from beginning and end of output

Is there any way of removing single quotes from the beginning and the end of the output string or list? I tried replace("'",""), but that does not seem to work for certain reason.

The replace function worked while processing the input but I am puzzled why it does not work for the output.

For example, the code block below:

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

    really_final_output = str(final_output).replace("'","")
    return really_final_output

should generate something along the lines of:

1, 2, 3, 4, 5, 6, 7, 8, 9

instead of:

'1, 2, 3, 4, 5, 6, 7, 8, 9'

>Solution :

You simply can’t remove it because if you remove it from a string it won’t be a string anymore.

To get only the values you must string.split(‘,’) and you will get a list of all elements which can then be converted to integers or floats.

string = '1, 2, 3'
numbers = list(map(int, string.split(',')))

The argument passed to split() is the character on which you want to split.

Now, if you are trying to get say all numbers from keyboard input you can do something like this:

numbers = list(map(int, input("Input message: ").split()))

Here a good reference to practice and learn more about the string split method: https://www.w3schools.com/python/ref_string_split.asp

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