Re: How to remove "" from starting of a string if provided by the user
On 2020-08-11 20:43, Dennis Lee Bieber wrote: On Tue, 11 Aug 2020 14:17:39 -0400, Joel Goldstick 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] -- https://mail.python.org/mailman/listinfo/python-list
Re: How to remove "" from starting of a string if provided by the user
On Tue, 11 Aug 2020, 02:20 Ganesh Pal, wrote: > The possible value of stat['server2'] can be either (a) > "'/fileno_100.txt'" or (b) '/fileno_100.txt' . > > How do I check if it the value was (a) i.e string started and ended > with a quote , so that I can use ast.literal_eval() > BAFP > def maybe_unquote(string): try: return ast.literal_eval(string) except ValueError: return string -- https://mail.python.org/mailman/listinfo/python-list
Re: How to remove "" from starting of a string if provided by the user
12.08.20 18:53, MRAB пише: > I would suggest: > while s[ : 1] in {'"', "'"} and s[ : 1] == s[-1 : ]: > ... s = s[1 : -1] And the condition can be written as s[ : 1] == s[-1 : ] in {'"', "'"} or more efficiently as s and s[0] == s[-1] in '\'"' -- https://mail.python.org/mailman/listinfo/python-list