Ever thought about writing a script which prints its own name when you execute it. It’s pretty simple. You must have noticed programs in which the main function is written like this
int main(int argc, char** argv)
and you must have wondered what these 2 arguments mean.
- Well the first one argc is the number of arguments passed in to your program.
- The second one argv is the array which holds the names of all the arguments passed into your program.
- Along with those arguments there is an extra piece of information stored in the array first cell of that array i.e. argv which is the full path of the file containing the code.
To print the name of the program all we need to do is to slice the filename out of that path.
Below is the python implementation of the idea discussed above. Let’s suppose the name of the script is print_my_name.
Note: The output will vary when you run it on GeeksforGeeks online compiler.
This article is contributed by Palash Nigam . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Python | Output using print() function
- Output of Python Program | Set 3
- Output of Python program | Set 5
- Output of Python Program | Set 1
- Program to Print K using Alphabets
- Program to print window pattern
- Program to print the diamond shape
- Python program to print Emojis
- Program to print all the numbers divisible by 5 or 7 for a given number
- Program to print all the numbers divisible by 3 and 5 for a given number
- Python program to print all even numbers in a range
- Python program to print even numbers in a list
- Python program to print odd numbers in a List
- Python program to print all odd numbers in a range
- Python Program to print digit pattern
- Python program to print calendar of given year
- Python program to print Aitken's array
- Python Program to Print Numbers in an Interval
- Python program to print all negative numbers in a range
- Python program to print the dictionary in table format
Improved By : Akanksha_Rai