'python adding extra slashes
I need to create an f-string with quotes.
I have the next code: query = f"UPDATE \"client_plan_feature\" SET subscription_end = \'{text_until_date}\' WHERE client_id={client_id} AND subscription_start IS NOT NULL", where
until_date is a string and client_id is an integer.
I want to get: 'UPDATE "client_plan_feature" SET subscription_end='2022-05-10' WHERE client_id=1 AND subscription_start IS NOT NULL',
but I'm getting the string with extra slashes, like: 'UPDATE "client_plan_feature" SET subscription_end=\'2022-05-10\' WHERE client_id=1 AND subscription_start IS NOT NULL', how can I fix it?
Solution 1:[1]
Just remove your slashes - '{text_until_date}'. You need to escape " because you used it as begin/end indicators, but you do not need to escape '.
You can use """ indicators too, so you do not need to use escape characters at all (even when you use newlines).
f"""UPDATE "client_plan_feature" SET subscription_end = '{text_until_date}' WHERE client_id={client_id} AND subscription_start IS NOT NULL"""
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | kosciej16 |
