Python input() method Tutorial
Hello in this tutorial, we will understand input()
method in python programming.
1. Introduction
Let us understand the input()
and raw_input()
method.
1.1 input() method
The input()
method in python programming reads input from the user, evaluates it (i.e. whether the user has entered a string, number, or a list), and returns it. If the input provided by the user is incorrect then python either throws a syntax error or exception. It is represented by the following syntax i.e. input([prompt_message])
.
1.1.1 How input() method works?
- The method expects input from the user and the program flow will be stopped until the user has entered the input
- The prompt message is displayed on the output screen asking the user to enter the value. This prompt message is optional
- Once the input is entered by the user, it is converted into a string
1.2 raw_input() method
The raw_input()
method is an old way of doing the same thing i.e. it was used in python 2.x. The method takes an input from the user, converts it to a string, and then returns it. It is represented by the following syntax i.e. raw_input([prompt_message])
.
1.3 Setting up Python
If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.
2. Python input() method Tutorial
I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of your choice.
2.1 Python input() method Tutorial
Let us see this method in action.
Python input() method
# python input tutorial # approach 1 - use of input() def user_input(): input_str = input('Enter your name: ') print('The entered string is = {}'.format(input_str)) # main() method def main(): print('\n--- Using input() approach ---\n') user_input() # driver code if __name__ == '__main__': main()
If everything goes well the following output screen will be shown in the IDE console where the program execution will be halted until the user enters input.
Console Output
--- Using input() approach --- Enter your name: daniel The entered string is = Daniel
2.2 Python raw_input() method Tutorial
Let us see this method in action. Remember to use the Python2.x if you want to use the raw_input()
method.
Python raw_input() method
# approach 2 - a use of raw_input() # raw_input() method only works in python2.x and is changed to input() in python3.x def user_raw_input(): input_str = raw_input('Enter your name: ') print('The entered string is = {}'.format(input_str)) # main() method def main(): print('\n--- Using raw_input() approach ---\n') user_raw_input() # driver code if __name__ == '__main__': main()
If everything goes well the following output screen will be shown in the IDE console where the program execution will be halted until the user enters input.
Console Output
--- Using raw_input() approach --- Enter your name: atlas The entered string is = atlas
That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!
3. Summary
In this tutorial, we learned:
input()
andraw_input()
methods in python programming to accept input from the user- Sample program to understand the
input()
andraw_input()
methods
You can download the source code of this tutorial from the Downloads section.
4. More articles
5. Download the Project
This was a python programming tutorial to understand the input()
and raw_input()
methods.
You can download the full source code of this example here: Python input() method Tutorial
Last updated on Apr. 29th, 2021