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,952
  • 5
  • 29
  • 52
  • 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

14

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

s = s.strip(' "\'\t\r\n')
Mark Byers
  • 767,688
  • 176
  • 1,542
  • 1,434
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
  • 8,760
  • 2
  • 41
  • 60