【问题标题】:How to accept string starting with # in python?如何在python中接受以#开头的字符串?
【发布时间】:2018-02-15 07:04:07
【问题描述】:

我正在开发一个 python 程序,我需要通过以下方式在终端中输入一个 hastag:

[delta@localhost Desktop]$ python CheckHastag.py #football

执行后会抛出错误 IndexError: 列表索引超出范围

这是因为python不接受以“#”开头的字符串,但是我尝试不使用#,即

[delta@localhost Desktop]$ python CheckHastag.py football

它有效。 那么如何让我的程序接受主题标签,即字符串开始
用#?

【问题讨论】:

    标签: python bash shell command-line command-line-arguments


    【解决方案1】:

    shell 将# 视为注释的开头,因此Python 解释器永远无法看到# 之后的内容。

    这可以使用echo 命令轻松演示:

    $ echo #football
    
    $ echo football
    football
    

    您有多种解决方法:

    $ python CheckHastag.py "#football"
    $ python CheckHastag.py '#football'
    $ python CheckHastag.py \#football
    

    【讨论】:

    • 非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2021-06-20
    • 2012-08-26
    • 2020-08-27
    • 1970-01-01
    • 2011-11-13
    • 2011-07-31
    • 2022-01-13
    • 1970-01-01
    相关资源
    最近更新 更多