简体   繁体   English

Python字符串百分号转义

[英]Python string percent sign escape

I am experimenting some string outputs and I came across something that throws an error when printing 我正在尝试一些字符串输出,我遇到了一些在打印时抛出错误的东西

x = "ll=%s%2C%20%s" % ("lat", "lng")

The syntax above throws an error: 上面的语法抛出一个错误:

ValueError: unsupported format character 'C' (0x43) at index 7

What am I missing here? 我在这里错过了什么? I wish to have a result of: 我希望得到一个结果:

ll=lat%2C%20lang

With the use of %s operators on concatenating a variable inside a string 使用%s运算符连接字符串中的变量

When python sees that % , it's expecting a formatting symbol right afterwards. 当python看到% ,它正在期待一个格式化符号。 Basically, it expects something like %s or %d ... But it finds a C , and it doesn't know what to do with that. 基本上,它期望像%s%d ...但它找到一个C ,它不知道该怎么做。

You can see what can you put after the % in this link . 您可以在此链接中看到%后面的内容。

If you want to have literally % in your string you have to escape it with another % : 如果你想在你的字符串中有字面上% ,你必须用另一个%逃避它:

>>> x = "ll=%s%%2C%%20%s" % ("lat", "lng")
>>> x
'll=lat%2C%20lng'

Note that in Python 3, this way is considered "outdated" in favor of the newer .format() method. 请注意,在Python 3中,这种方式被认为是“过时的”,有利于较新的.format()方法。 You can also use that one in Python 2.7 (I believe , though I'm not sure that it was introduced in Python 2.6 ?) and do the same like this: 你也可以在Python 2.7中使用它(我相信 ,虽然我不确定它是在Python 2.6中引入的吗?)并且像这样做:

>>> x = "ll={0}%2C%20{1}".format("lat", "lng")
>>> x
'll=lat%2C%20lng'

Or you could do even fancier things: 或者你甚至可以做更好的事情:

>>> x = "ll={latitude}%2C%20{longitude}".format(latitude="lat", longitude="lng")
>>> x
'll=lat%2C%20lng'

Check it out! 看看这个! (also, there's a Reddit thread about it) (还有一个关于它的Reddit主题

First of all if you want to print % you need to do it like this 首先,如果你想打印%你需要像这样做

%% --> escapes the % literal character %% - >转义%文字字符

every other combinations will be treated as formatted characters. 每个其他组合将被视为格式化字符。 for example %c is treated as a single character, represented as a C int. 例如, %c被视为单个字符,表示为C int。

Please refer to the link here 请参阅此处的链接

To escape the % in python, just use %% , in your example, the following will give the result you want, 要在python中转义% ,只需使用%% ,在您的示例中,以下将给出您想要的结果,

x = "ll=%s%%2C%%20%s" % ("lat", "lng")

Or you could use string's format method which is preferred in python 3 and also available in python 2.7 或者你可以使用字符串的格式方法,它在python 3中是首选,也可以在python 2.7中使用

x = "ll={0:s}%2C%20{1:s}".format("lat", "lng")

One tip for you to transit from % style formatting to string's format method, AFAIK, all % format letters remain the same used in string's format method. 您可以从%样式格式转换为字符串的格式方法AFAIK,所有%格式字母都保留在字符串格式方法中使用的相同提示。 This means "%s" % "lat" would simply become "{0:s}".format("lat") , "%d" % 3 to "{0:d}".format(3) , and etc. Notice the 0 here. 这意味着"%s" % "lat"将简单地变为"{0:s}".format("lat")"%d" % 3"{0:d}".format(3)等等注意这里的0 It indicates which parameter in the format method is formatted, with first parameter indexed as 0. 它指示format方法中的哪个参数被格式化,第一个参数索引为0。

See more details here on the official documentation about python string's format method 有关python string的格式方法的官方文档,请参阅此处的更多详细信息

aside from % escape. 除了%逃脱。 you can also add the '%' in the 'lat' before passing it or just add another '%s' for '%' 您也可以在传递之前在'lat'中添加'%',或者只为'%'添加另一个'%s'

>>> x = "ll=%s2C%s20%s" % ("lat%", "%","lng")
>>> x
'll=lat%2C%20lng'

im just giving you another option.but %% escape is the best choice 我只是给你另一个选择。但%%逃脱是最好的选择

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

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