3

Possible Duplicate:
Best way to strip punctuation from a string in Python

I have a string I want to put between 2 double quotes on output "{{ var }}", so I want to make sure all single/double quotes are removed from the ends of the user-supplied string. What is the most efficient way to accomplish this?

Inputs / desired outputs:

   """""""""" string here '''''''''''''''         => 'string here'
       string'''''''""""""''''''"""""  => 'string'
Community
  • 1
  • 1
chrickso
  • 2,994
  • 5
  • 30
  • 53
  • 1
    Would like to see a duplicate. Everything found does not specify multiple instances of characters. – chrickso Aug 23 '12 at 19:29

2 Answers2

16

Use strip with a string containing the characters you wish to strip from the ends:

s = s.strip(' "\'\t\r\n')
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
3
from string import whitespace
new_string = string.strip(whitespace + '"\'')

Using the strip method, with the whitespace constant to start off your list of characters to remove.

supervacuo
  • 9,072
  • 2
  • 44
  • 61