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

MRAB python at mrabarnett.plus.com
Wed Aug 12 11:53:27 EDT 2020


On 2020-08-11 20:43, Dennis Lee Bieber wrote:
> On Tue, 11 Aug 2020 14:17:39 -0400, Joel Goldstick
> <joel.goldstick at gmail.com> declaimed the following:
> 
[snip]

> Warning -- it will fail if the input is just a pair of quotes or pair of
> apostrophes -- improvement is
> 
>>>> while s and s[0] in ['"', "'"]:
> ... 	if s[0] == s[-1]:
> ... 		s = s[1:-1]
> 
If the 'if' condition is False, then 's' will remain unchanged, and it 
will loop forever.

I would suggest:

 >>> while s[ : 1] in {'"', "'"} and s[ : 1] == s[-1 : ]:
...     s = s[1 : -1]


More information about the Python-list mailing list