V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
yyrhwy
V2EX  ›  Python

[ Python ]请问这段代码中 value = value.strip()这行起到什么作用呢?

  •  
  •   yyrhwy · Apr 23, 2019 · 2959 views
    This topic created in 2561 days ago, the information mentioned may be changed or developed.
    import re
    states = [' Alabama ', 'Georgia!', 'Georgia', 'georgia', 'FlOrIda',
    'south carolina##', 'West virginia?']


    def clean_strings(strings):
    result = []
    for value in strings:
    value = value.strip()
    value = re.sub('[!#?]', '', value)
    value = value.title()
    result.append(value)
    return result


    print(clean_strings(states))

    为了使 states 中的数据整齐,所以需要清洗数据。
    其中 value = value.strip()这行代码似乎可有可无,因为即使将其注释掉,输出结果依然没有变化。
    .strip()方法的作用是什么呢?
    qinghon
        1
    qinghon  
       Apr 24, 2019 via Android
    python strip
    Google 请
    banixc
        2
    banixc  
       Apr 24, 2019
    smallpython
        3
    smallpython  
       Apr 24, 2019
    搜一下 strip ()这个函数有什么作用就可以了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1028 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 19:01 · PVG 03:01 · LAX 12:01 · JFK 15:01
    ♥ Do have faith in what you're doing.