Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python string formatting single quotes and double quotes

I have a variable state = ‘PA’. I am trying to generate a string as follows. I would like add single quotes on the state within a string. Also, I want to use this .format method because I will change this state later.

‘select * from table where "state" = ‘PA”

Currently, I could only be able to generate this
‘select * from table where "state" = PA’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

using the following code:

'select * from table where "state" = {}'.format(state)

>Solution :

You can escape the single quotes around the format specifier like this:

>>> s = 'select * from table where "state" = \'{}\''.format(state)
>>> print(s)
select * from table where "state" = 'PA'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading