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
5TuNan
V2EX  ›  Python

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

  •  
  •   5TuNan · 2020-09-16 21:18:37 +08:00 · 1413 次点击
    这是一个创建于 1311 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    also24
        3
    also24  
       2020-09-16 21:28:43 +08:00
    ipwx
        4
    ipwx  
       2020-09-16 21:33:38 +08:00
    楼主你…… 居然以为 find 的第二个参数是“寻找第二个出现的 xxx”。。。
    chocovon
        5
    chocovon  
       2020-09-16 21:34:22 +08:00
    第一个怎么打印出 4 的……
    ipwx
        6
    ipwx  
       2020-09-16 21:35:56 +08:00
    @chocovon
    @SingeeKing 肯定有不可见 unicode 字符呗。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1989 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 16:14 · PVG 00:14 · LAX 09:14 · JFK 12:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.