Question

I have two strings.

dat: "13/08/08
tim: 12:05:51+22"

I want the " character stripped from both the strings. Here is the code I am using:

dat=dat.strip('"')
tim=tim.strip('"')

The resulting strings I get are:

dat: 13/08/08
tim: 12:05:51+22"

Why is the " character not removed from tim?

According to the documentation here (http://www.tutorialspoint.com/python/string_strip.htm) it should work.

Was it helpful?

Solution

According to the docs, strip([chars]):

Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed.

So, " won't be replaced from dat: "13/08/08 and will be replaced from tim: 12:05:51+22" because here " is at the end:

>>> dat = 'dat: "13/08/08'
>>> tim = 'tim: 12:05:51+22"'
>>> dat.strip('"')
'dat: "13/08/08'
>>> tim.strip('"')
'tim: 12:05:51+22'

Use replace() instead:

>>> dat.replace('"', '')
'dat: 13/08/08'
>>> tim.replace('"', '')
'tim: 12:05:51+22'

OTHER TIPS

Seem to work here

>>> tim2 = "tim: 12:05:51+22\""
>>> print tim2
tim: 12:05:51+22"
>>> tim = tim2.strip('"')
>>> print tim
tim: 12:05:51+22
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top