Home >Backend Development >Python Tutorial >How to use Python's command line input method

How to use Python's command line input method

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBforward
2023-05-10 12:34:062361browse

Command line input

Python allows command line input.

This means we can ask for input from the user.

The approach in Python 3.6 is slightly different than in Python 2.7.

Python 3.6 uses the input() method.

Python 2.7 uses the raw_input() method.

The following example will ask the user for their name, and when the name is entered, the name will be printed to 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 the name:

Bill

Then, the program will print a message:

Hello, Bill

The above is the detailed content of How to use Python's command line input method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete