We should know by now that we will want to be able to pass command-line arguments to Python and we can do this using the
argv array similar to Perl. However, we are more like bash, with Python we combine the program name into the array with the other arguments. Python also uses lowercase instead of uppercase in the object name.
argvarray is a part of the
sys.argvis the script name
sys.argvis the first argument supplied to the script
sys.argvis the second supplied argument and so on
The argument count will always be at least 1, so, keep this in mind when checking for supplied arguments
#!/usr/bin/python3 import sys print("Hello " + sys.argv)
If we run the script with a supplied argument, we should be able to see something similar to the following screenshot:
Our code is still quite clean and simple; however...