简体   繁体   中英

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'

using the following code:

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

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM