在python中如何接受以#开头的字符串?
我正在开发一个python程序,需要以以下方式在终端中输入hastag:在python中如何接受以#开头的字符串?,python,bash,shell,command-line,command-line-arguments,Python,Bash,Shell,Command Line,Command Line Arguments,我正在开发一个python程序,需要以以下方式在终端中输入hastag: [delta@localhost Desktop]$ python CheckHastag.py #football 执行后,它抛出错误,如下所示 索引器:列表索引超出范围 这是因为python不接受以“#”开头的字符串,但是我尝试了不使用#,即 [delta@localhost Desktop]$ python CheckHastag.py football 它起作用了。 那么,如何使我的程序接受hashtag,即字
[delta@localhost Desktop]$ python CheckHastag.py #football
执行后,它抛出错误,如下所示
索引器:列表索引超出范围
这是因为python不接受以“#”开头的字符串,但是我尝试了不使用#
,即
[delta@localhost Desktop]$ python CheckHastag.py football
它起作用了。
那么,如何使我的程序接受hashtag,即字符串开始使用#?时,shell将
视为注释的开头,因此Python解释器永远无法看到
后面的内容
这可以使用echo
命令轻松演示:
$ echo #football
$ echo football
football
您有几个解决此问题的选项:
$ python CheckHastag.py "#football"
$ python CheckHastag.py '#football'
$ python CheckHastag.py \#football
非常感谢你。