How to remove "" from starting of a string if provided by the user

Cameron Simpson cs at cskk.id.au
Mon Aug 10 17:06:23 EDT 2020


On 11Aug2020 00:05, Ganesh Pal <ganesh1pal at gmail.com> wrote:
>How to remove " from the starting and ending of a string , before
>comparison . Here is an example and my solution wtih eval ( I am advised
>not to use this one) , please suggest an alternative . I am on linux and
>python 2.7

Indeed. Using eval is extremely dangerous - it can execute _any_ python 
code at all. Do not do that.

I would look for the quote character (you say " above but all your 
examples actually use the ' character) at the start and end with 
.startswith and .endswith methods, and cut out the middle with a slice 
if they are both your target quote character (don't forget that Python 
supports negative indices, making this pretty easy).

But I suspect that in the real world you'll find this approach 
simplistic. Normally quotes introduce a special syntax between them, 
such a \n to indicate a newline character and so forth, so _only_ 
stripping the quotes is not al that would be required.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list