uNIKx
uNIKx

Reputation: 123

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 # ?

Upvotes: 3

Views: 72

Answers (1)

NPE
NPE

Reputation: 500357

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

Upvotes: 9

Related Questions