推荐学习书目
› 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
5TuNan
V2EX  ›  Python

当字符串首位是空格时,使用 str.find()的返回值与普通字符串不一致。

  •  
  •   5TuNan · Sep 16, 2020 · 2448 views
    This topic created in 2200 days ago, the information mentioned may be changed or developed.

    Python 中字符串中有空格时,使用 str.find() 与 没有空格使用 find,返回的值是不一样的。有没有大佬给解释一下为什么,非常感谢。 代码如下

    a = 'afbf'
    print(a.find('f', 2))  # 打印: 4
    b = ' afbf'
    print(b.find('f', 2))  # 打印: 2
    
    also24
        1
    also24  
       Sep 16, 2020
    是不是没注意 find 方法的第二个参数?
    SingeeKing
        2
    SingeeKing  
       Sep 16, 2020
    难道市面上出现了盗版的 Python……

    ipwx
        4
    ipwx  
       Sep 16, 2020
    楼主你…… 居然以为 find 的第二个参数是“寻找第二个出现的 xxx”。。。
    chocovon
        5
    chocovon  
       Sep 16, 2020
    第一个怎么打印出 4 的……
    ipwx
        6
    ipwx  
       Sep 16, 2020
    @chocovon
    @SingeeKing 肯定有不可见 unicode 字符呗。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2641 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 15:09 · PVG 23:09 · LAX 08:09 · JFK 11:09
    ♥ Do have faith in what you're doing.