How to take user input(both integer and string) in python ?

In this post, we will look at an interesting part of any programming language.

Yes, you guessed it right. We will read about how to take input from a user and print it in python.

To take user input we have “input()” function available in python.

Lets see an example to understand it.

Taking user input of a String & Integer

user@ngelinux$ cat input.py 
#!/usr/local/bin/python3 
name=input("Please enter your name : ")
age=input("Your age : ")
print ("Welcome ", name, ". You are", age, "years old")
user@ngelinux$ 

Output

user@ngelinux$ ./input.py 
Please enter your name : saket
Your age : 12
Welcome  saket . You are 12 years old

In the above example, we can see input function is used to take input of string and integer both.

So now its very easy to take user input and to understand the program.

Earlier we have raw_input() function for string input and input() function to take only integer input.
Now raw_input() is renamed to input()
And all user input is read by input function.

Leave a Reply

Your email address will not be published.