susja
susja

Reputation: 321

Pass parameter from command line in python

I have a simple task and want to run command from the line. E.g.

python3 -c 'print(2*2)'

The issue is when I want to invoke a function and pass parameter to it. E.g I want to lower the string 'ABC'. I use

python3 -c 'print(x="ABC",x.lower())'

Hence my question: how could I pass string value to function when invoke python from command line?

Upvotes: 1

Views: 145

Answers (1)

ForceBru
ForceBru

Reputation: 44828

Use sys.argv to obtain command-line arguments:

$ python3 -c "import sys; print(sys.argv[1].lower())" HELLO
hello
$

Upvotes: 1

Related Questions