2

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.

2 Answers 2

1

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'
Sign up to request clarification or add additional context in comments.

5 Comments

OP's result is weird.
Yes, I guess OP has just mixed up strings.
Or, OP is using "13/08/08 as dat and 12:05:51+22" as tim.
I guess there may be a space at the end of tim.
I thought it was pretty weird myself. Anyway this replace seems to work so that's fine :) Thank you.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.