Python Command Line Input
- Previous Page Python Try Except
- Next Page Python String Formatting
Elective Course
Course Recommendation:
Command Line Input
Python allows command line input.
This means we can ask users to input. The method in Python 3.6 is slightly different from Python 2.7.
Method.
input() used in Python 3.6 raw_input() used in Python 2.7
Method.
The following example will ask for the user's name, and when you enter your name, it will be printed on the screen:
Python 3.6
print("Enter your name:") x = input() print("Hello ", x)
Python 2.7
print("Enter your name:") x = raw_input() print("Hello ", x)
Save this file as demo_string_input.py
, and load it via the command line:
C:\Users\Your Name>python demo_string_input.py
Our program will prompt the user to enter a string:
Enter your name:
Now the user enters their name:
Bill
Then, the program will print a message:
Hello, Bill
- Previous Page Python Try Except
- Next Page Python String Formatting