简体   繁体   English

python 字符串格式化单引号和双引号

[英]python string formatting single quotes and double quotes

I have a variable state = 'PA'.我有一个变量 state = 'PA'。 I am trying to generate a string as follows.我正在尝试生成一个字符串,如下所示。 I would like add single quotes on the state within a string.我想在字符串中的 state 上添加单引号。 Also, I want to use this.format method because I will change this state later.另外,我想使用 this.format 方法,因为稍后我将更改此 state。

'select * from table where "state" = 'PA'' 'select * from table where "state" = 'PA''

Currently, I could only be able to generate this 'select * from table where "state" = PA'目前,我只能生成这个 '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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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