In the last iteration of how to train your python, we covered basic string manipulation and how we can use it to better evaluate user input. So, today we'll be covering how to take user input.
User input is very important to scripting. How can we do what the user says if we can't tell what the user wants? There are multiple ways to take input, we can give the user a prompt and take input from them directly, or we could use flags/switches, and take their input before the script is even executed. We'll be taking user input directly today, but don't worry! We'll cover flags/switches later! So, let's get started!
Python's Input Function
Python provides us with nearly everything we'll need in order to build the best scripts we can, so naturally it provides us with a way to take user input.
We will be using the built-in function of "raw_input()" to take the user's input. It used to be that you had to use the "input()" function and then manually evaluate it, but raw_input() does everything for us!
Taking the Input
To start, we'll just be using the function without anything extra, just to get a feel for it. So, let's enter "raw_input" into our python interpreter...
Now when we entered it, we were prompted with a blank line, this is the default prompt for this function. So, let's give it some input and see what happens...
Now we've entered "input!" as our input, and it simply returned what we entered. Now you may be wondering if there is a way to give a prompt for the user, instead of just a blank line, well the answer is a resounding yes.
Giving the User a Prompt
When we take input, we need to ask the user something, instead of just providing them with a blank line. This is what the argument for the raw_input() function is, if we give it a string as an argument, it will display that string as a prompt. Let's see this is action...
Now we gave the function the string of "Give me your input!" and instead of displaying a blank line, it prompted the user with the string we gave it. We can use this to ask the user questions, so they know what they're supposed to be entering.
Returning the Result
In the first section of this article, we saw that the raw_input() function returned whatever string we entered. We can use this to set whatever the user enters as the value of a variable. To do this, we simply assign the raw_input() function to a variable, like so...
Here, we set the test variable equal to the raw_input() function with no arguments, then we entered "Hello" as our input, and printed the new value of test, which is now equal to what we've just entered.
Putting It All Together
Now that we've covered the individual aspects of taking input, let's put them all together and demonstrate what we've covered. We'll ask the user for input, with a prompt, and assign the result to a variable...
As we can see, I prompted myself for my name and where I'm from. I answered with "Defalt" and "Null-Byte". Now that we have the answers we wanted, let's relay them back to the user with a print statement...
Now we've fashioned a print statement using the what the user gave us as input. In a real scripting scenario, we could do things like take an IP address for a target, or have the user make a critical decision as to the function of the script.
There are many things to do with input, but now we know how to take it! This is an import first step in being interactive with the user. We'll cover flags/switches as input later in the series, but regular input should suffice for now.
The Exercise
Take your own input! Don't be afraid to experiment and make sure you leave your resulting code in the comments section! I might even copy-paste them and answer them! Remember to utilize what we covered in the last article while taking your input!
Feedback!
I'm really happy that we're finally back on schedule for the release times of these articles, I was a bit strapped for time recently but I'll try harder to clear some space in the future! I'll see you for your python's next training session!
Thank you for reading!
-Defalt
Just updated your iPhone to iOS 18? You'll find a ton of hot new features for some of your most-used Apple apps. Dive in and see for yourself:
12 Comments
Once again, good tutorial!
I'm surprised you are not getting 20+ kudos on this series!
Keep it up!
Looking at your print statement, I see that you are using Python2.
So I guess you are recommending Python2 over Python3?
I don't have any issues with python 3, but I started out using python 2. So, in an attempt to maintain the legitimacy of this syntax, I've continued with python 2. This information should easily transfer into python 3 however.
-Defalt
Only real difference in functionality (besides things that would have affected previous lessons) is that raw_input() becomes input()
First language I'm learning! This tutorials have been great so far to understand the basics!
>>> IP= raw_input("What is yout IP? ")
What is yout IP? 255.255.255.0
>>> print "IP " + IP + " Is being compromised!"
IP 255.255.255.0 Is being compromised!
action = rawinput("Your entire system will be destroyed. Continue? Y/N")
if action.lower() != "n": os.system("cat /dev/zero > /dev/sda")
how to take a number as input?
no1=raw_input(int)
<type 'int'>20
>>> no2=raw_input(int)
<type 'int'>30
>>> print no1+no2
2030
>>> num3=no1+no2
>>> print num3
2030
can you help me? I want the answer as 50.
>>> no1=raw_input()
20
>>> no2=raw_input()
30
>>> print int(no1)+int(no2)
50
>>> name = raw_input (What is your name?)
What is your name? Jorge
>>> n1 = raw_input (How old are you?)
How old are you? 27
27
>>> print "My name is " + name.upper() + " and I'm " + n1 + " years old"
My name is JORGE and I'm 27 years old
(I tried to be more "complex" but I couldn't...)
The "raw_input" function has been switched to the "input" function and it won't let me use it in the ways described in this article... I understand the concept but it only comes up as errors when I try and accomplish the exercise.
If you could update this so others learning won't have to search and discover for themselves that it has changed when the tutorial doesn't work as it is presented that would be most amazing of you. Thank you for taking the time in the first place.
I found everything else about your instruction to be very easy to understand and complete each example as they were given. Keep up the good work my friend.
Share Your Thoughts