How to use Python's command line input method

WBOY
Release: 2023-05-10 12:34:06
forward
2116 people have browsed it

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)
Copy after login

Python 2.7

print("Enter your name:")
x = raw_input()
print("Hello ", x)
Copy after login

Save this file as demo_string_input.py and load it via the command line:

C:\Users\Your Name>python demo_string_input.py
Copy after login

Our program will prompt the user to enter a String:

Enter your name:
Copy after login

Now the user enters the name:

Bill
Copy after login

Then, the program will print a message:

Hello, Bill
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!