[Python 语法] 询问在 String 中关于 % 的用法(找不到相关文档)

2016-01-25 22:41:07 +08:00
 terence4444
请问以下的用法相关文档可以在哪里找到?我想知道是否关于 % 还有其它用法。

string1 = 'string replace %s'
string2 = 'abcdefg'
string3 = string1 % string2
>> string3 == 'string replace abcdefg'

谢谢
4269 次点击
所在节点    Python
36 条回复
fy
2016-01-26 11:25:37 +08:00
@necomancer
@qihboy

虽然 PEP3101 并没有推荐不推荐的内容, str.format 的 3.5 版本文档里也没有这句话,但我在别处发现了一段信息:
https://docs.python.org/3.5/library/stdtypes.html#printf-style-string-formatting

Note: The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer str.format() interface helps avoid these errors, and also provides a generally more powerful, flexible and extensible approach to formatting text.

他说元组和字典的显示有问题,然而貌似并没有:

>>> '%s and %s a' % ((1,2,3), {1:2, 'a':2})
"(1, 2, 3) and {1: 2, 'a': 2} a"

不知道为什么官方文档会有如此明显的倾向性,还没给出合适的例子。我个人觉得 .format 作为一个复杂蛋疼的机制,还是太冗长,不讨喜。
youngsterxyf
2016-01-26 13:12:40 +08:00
hitmanx
2016-01-26 13:19:21 +08:00
说找不到也是蛮奇怪的事,我 google "python string percentage sign",前 5 条都是关于这个的,其中第 5 条就是官方doc.
terence4444
2016-01-26 13:48:43 +08:00
@hitmanx 我用 "Python String Operator %" 找的…… 以为是一个 operator ,昨天在家用的 Bing
hitmanx
2016-01-26 16:31:08 +08:00
@terence4444 不是我较真啊.按照你的关键词搜,无论是 bing 还是 google 前几个结果都是关于这个的啊,莫非是我这里搜索多了排名有优化?

bing 的结果如下..除了第二个可能不对以外,其他的每个都包含了 "% operator", "string formatting"之类的关键字.

7.1. string — Common string operations — Python …
https://docs.python.org/2/library/string
The string module contains a number ... Python ’ s built-in string classes support ... To output formatted strings use template strings or the % operator described in ...

python convert a string to an operator - Stack Overflow
stackoverflow.com/.../5117112/python-convert-a-string-to-an-operator
RESOLVEDLAST UPDATED: 2/25/20113 POSTSFIRST POST: 2/25/2011
Is it possible to convert a string to an operator in python? I would like to pass a condition to a function Ideally it would look like this: def foo(self, attribute ...

performance - Python string formatting: % vs. .format ...
stackoverflow.com/questions/5082452
RESOLVEDLAST UPDATED: 12/23/20138 POSTSFIRST POST: 2/22/2011
Python 2.6 introduced the str.format() method with a slightly different syntax from the existing % operator. Which is better and for what situations? The following ...

5. Built-in Types — Python 2.7.11 documentation
https://docs.python.org/2/library/stdtypes
To output formatted strings use template strings or the % operator described in the String Formatting Operations section. ... String (converts any Python object using str
Python Strings - Tutorialspoint

www.tutorialspoint.com/python/python_strings.htm
String Formatting Operator. One of Python's coolest features is the string format operator %. This operator is unique to strings and makes up for the pack of having ...
se77en
2016-01-26 16:43:20 +08:00
最全的解释 https://pyformat.info
terence4444
2016-01-26 17:16:40 +08:00
@hitmanx 我找的结果好像和你贴的不一样,不过即使一样也没有很多相关信息把引导到 format 上去

7.1. string — Common string operations — …翻译此页
The string module contains a number ... Python ’ s built-in string classes support ... To output formatted strings use template strings or the % operator described in ...

python convert a string to an operator - Stack Overflow
最佳答案 Is it possible to convert a string to an operator in python? I would like to pass a condition to a function Ideally it would look like this: def foo(self, attribute ...详情
stackoverflow.com/.../5117112/python-convert-a-string-to-an-operator  ·  2011-02-25

9.9. operator — Standard operators as functions …翻译此页
This table shows how abstract operations correspond to operator symbols in the Python syntax and the functions in the ... String Formatting: s % obj: mod(s, obj ...

5. Built-in Types — Python 2.7.11 documentation 翻译此页
To output formatted strings use template strings or the % operator described in the String Formatting Operations section. ... String (converts any Python object using str

BitwiseOperators - Python Wiki 翻译此页
These are Python's bitwise operators. ... they treat it as if it were a string of bits, ... Python allows operator overloading, ...
https://wiki.python.org/moin/BitwiseOperators
Operators and String Formatting in Python - …翻译此页
Operators and String Formatting . Terms in This Chapter . Format directives; Hexdump; Key; Keyword; Literal; Modulus; Operator precedence; Boolean value; Class ...
www.informit.com/articles/article.aspx?p=28790

在第一个搜索结果中并没有 format 这样的用法,我都是打开然后直接搜索 % 符号找的,知道 % 和 format 是一样的话应该可以找到,但是不知道的话,我觉得并不是那么容易。
necomancer
2016-01-26 22:05:51 +08:00
@fy

a = (1, 2)
print("position is %s" % name) 会报错,需要
(name, )的写法吧。
necomancer
2016-01-26 22:07:22 +08:00
@fy

BTW 我个人也倾向 %, 感觉更直白简单一点
necomancer
2016-01-26 22:11:03 +08:00
我了个脑残,变量名都弄错了, V2EX 好像也不能改
MinskyNg
2016-01-26 22:29:29 +08:00
新版的 python 教程好像都推荐 format,功能更强大
kkzxak47
2016-01-26 22:38:42 +08:00
format 更简单,开始用就会喜欢上它
Arthur2e5
2016-01-26 22:46:16 +08:00
@terence4444 第一种 %(foo)s 指定了字段的名称,经常能让东西更可读(特别是翻译软件的时候译者可以直接看到这里是什么东西)——相应地你应该在 format 里面用 "{foo}".format(foo='bar')。

@ethego 考虑类型…… Python 还 Better Explicit than Implicit 呢。主要是 format 功能性重要啦。
ming2281
2016-01-26 22:55:20 +08:00
楼主用过 ipython 没
如果没有,推荐试试,大部分问题都会烟消云散
你这种查帮助文档的问题,在 ipython 里面都不值一提

它非常强大,强大到它变成了我现在的默认 linux shell
terence4444
2016-01-31 17:30:20 +08:00
@ming2281 这台电脑我还要打游戏……公司里摸鱼写 Python 的电脑也是 Windows 的,所以没有 Linux ……
terence4444
2016-02-14 18:09:27 +08:00
在附言 1 中总结了这帖的内容,应该算是结帖了,撒花。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/253341

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX