如何在python中接受以#开头的字符串?

How to accept string starting with # in python?

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

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

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

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

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

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

shell 将 # 视为注释的开始,因此 Python 解释器永远看不到 #.[=15= 之后的内容]

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

$ echo #football

$ echo football
football

您有多种解决此问题的方法:

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