Python List to String Conversion

smada picture smada · Jan 28, 2013 · Viewed 33.2k times · Source

This seems like a simple task and I'm not sure if I've accomplished it already, or if I'm chasing my tail.

values = [value.replace('-','') for value in values] ## strips out hyphen (only 1)
            print values ## outputs ['0160840020']
            parcelID = str(values) ## convert to string
            print parcelID ##outputs ['0160840020']
            url = 'Detail.aspx?RE='+ parcelID ## outputs Detail.aspx?RE=['0160840020']

As you can see I'm trying to append the number attached to the end of the URL in order to change the page via a POST parameter. My question is how do I strip the [' prefix and '] suffix? I've already tried parcelID.strip("['") with no luck. Am I doing this correctly?

Answer

David Robinson picture David Robinson · Jan 28, 2013

values is a list (of length 1), which is why it appears in brackets. If you want to get just the ID, do:

parcelID = values[0]

Instead of

parcelID = str(values)