V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
MartinWu
V2EX  ›  Python

刚发现 Python 一个现象,求解释

  •  
  •   MartinWu ·
    Martwu · 2019-04-11 14:07:44 +08:00 · 2633 次点击
    这是一个创建于 1813 天前的主题,其中的信息可能已经有所发展或是发生改变。
    long_str = ("This is a long %s" % "story." "And you are welcome to %s." % "listen")
    

    是没有语法错误的。 然后,

    long_str = ("It is %d stories." % 3 )
    

    也是没问题的。 最后,

    long_str = ("我编不下去了,随便搞%d 个测试" % 123 "这是一个%s 测试啊" % "有趣的")
    

    就是说这种方式来组合字符串, % 后的元素穿插在中间的,只能是字符串,其他类型元素,只能安排在最后。 不然会报语法错误。大家知道是那部分的 python 文档解释到吗?

    第 1 条附言  ·  2019-04-11 15:08:53 +08:00
    看了各位大佬的回复,我觉得是 @Alexhex 大佬才说到点子上(或者说,解答了我的疑惑)。实际上就是运算优先级的问题,导致了我的疑惑。谢谢各位解惑的大佬。
    第 2 条附言  ·  2019-04-11 15:10:23 +08:00
    啊,上面 @ 错了, 是 @wnh3yang 大佬才对!
    第 3 条附言  ·  2019-04-11 15:25:22 +08:00

    我觉得重点是:

    只用空格分开的两个字符串,会被解释成一个字符串。

    其实这其中也就不涉及运算优先级的问题了。

    12 条回复    2019-04-11 15:01:38 +08:00
    wnh3yang
        1
    wnh3yang  
       2019-04-11 14:15:23 +08:00   ❤️ 1
    我不是程序员,但我看出来错误了。

    <img src="https://img.vim-cn.com/ff/7eeb833337ed48124facf6489efb169a13a141.png" alt="">

    两个字符串放一起会自动拼成一个字符串, 数字和字符串放一起报语法错误。
    lithiumii
        2
    lithiumii  
       2019-04-11 14:17:45 +08:00
    % 是用来把变量插到字符串里面的,当然是完整的字符串写前面,别的扔后面……
    但是,我从来没学会过用 %,跟谜语似的。反正你写 py 的话建议用 str.format(),比如:

    long_str = ("我编不下去了,随便搞{}个测试这是一个{}测试啊".format(123, "有趣的"))

    https://docs.python.org/3.7/library/string.html#formatstrings
    MartinWu
        3
    MartinWu  
    OP
       2019-04-11 14:21:47 +08:00
    @lithiumii #2 但是我觉得你的这个解释没法解释为什么:

    ``` python
    long_str = ("This is a long %s" % "story." "And you are welcome to %s." % "listen")
    ```

    语法没报错,这也是中间穿插了一个 % 渲染,只是这里是"story" 而不是 123。
    besto
        4
    besto  
       2019-04-11 14:23:32 +08:00
    123 '错误'
    '123' '正确'

    好理解一点了么
    Alexhex
        5
    Alexhex  
       2019-04-11 14:26:35 +08:00
    你试着 print 一下第一个。
    Alexhex
        6
    Alexhex  
       2019-04-11 14:31:49 +08:00
    说错了,你试着 print 一下下面这个:

    long_str = ("This is a long %s, but" % "story." " you are welcome to %s." % "listen")
    wnh3yang
        7
    wnh3yang  
       2019-04-11 14:33:26 +08:00   ❤️ 1
    @MartinWu

    我觉得是运算优先级的问题:

    ``` python
    long_str = ("This is a long %s" % "story." "And you are welcome to %s." % "listen")
    ```

    "story." "And you are welcome to %s." 最先合并成一个字符串。会变成:
    ``` python
    long_str = ("This is a long %s" % "story.And you are welcome to %s." % "listen")
    ```

    再变成
    ``` python
    long_str = ("This is a long %s" % "story.And you are welcome to listen.")
    ```

    再变成
    ``` python
    long_str = ("This is a long story.And you are welcome to listen.")
    ```


    所以
    ``` python
    long_str = ("我编不下去了,随便搞%d 个测试" % 123 "这是一个%s 测试啊" % "有趣的")
    ```

    123 "这是一个%s 测试啊"

    这个数字和字符串合并的过程会报语法错误
    LFly
        8
    LFly  
       2019-04-11 14:36:55 +08:00
    print(123 "long str")
    print("123" "long str")
    long_str = ("我编不下去了,随便搞%s 个测试" % 123, "这是一个%s 测试啊" % "有趣的")
    Tink
        9
    Tink  
       2019-04-11 14:37:41 +08:00
    "123"
    lihnzx
        10
    lihnzx  
       2019-04-11 14:42:03 +08:00
    "我编不下去了,随便搞%d 个测试" % 123 + "这是一个%s 测试啊" % "有趣的"

    没明白你的意思
    拼接字符串使用 format 或 join 性能更高一点
    rrfeng
        11
    rrfeng  
       2019-04-11 14:50:03 +08:00 via Android
    ("aaaa" "bbbbb") == ("aaaabbbbb")
    (3 "aaaaaa") = error


    看懂这个就好了跟 % 没关系
    lithiumii
        12
    lithiumii  
       2019-04-11 15:01:38 +08:00
    @MartinWu
    圆括号里面放字符串并且不加逗号,视同一个长的字符串,放别的类型不行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3246 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 12:16 · PVG 20:16 · LAX 05:16 · JFK 08:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.