Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to accept string starting with # in python?

I am working on a python program where I need to enter a hastag in the terminal in following manner:

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

after executing it throws out error as IndexError: list index out of range

It is because python is not accepting string starting with "#", however I tried without # i.e.

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

it works. So how do I make my program to accept the hashtag i.e string starting
with # ?

like image 277
uNIKx Avatar asked Apr 21 '26 19:04

uNIKx


1 Answers

The shell treats # as the start of a comment, so the Python interpreter never gets to see what comes after the #.

This can be easily demonstrated using the echo command:

$ echo #football

$ echo football
football

You have several options for working around this:

$ python CheckHastag.py "#football"
$ python CheckHastag.py '#football'
$ python CheckHastag.py \#football
like image 123
NPE Avatar answered Apr 23 '26 08:04

NPE