Saturday, September 4, 2021

How To Take User Input From The Command Line

How To Take User Input From The Command Line

Python provides developers with built-in functions that can be used to get input directly from users and interact with them using the command line (or shell as it is often called). Python 2 programmers use the function raw_input() to accomplish this task. Python 3 introduced the new input() function. The two functions have the same semantics.

Consider the following Python 3 code:

name = input('Enter your name: ')

This will display the sentence ‘Enter your name:’ as a prompt in the shell. Python will then wait until the user types something on the command line, followed by hitting the enter key. Whatever the user types will be the return value of the input() function. It is important to note that this value will always be treated as a string by Python. In our current example, the string will be assigned to the variable ‘name’. To produce the same result in

(continued...)

from Planet SciPy
read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...