Please support if you like my work by payment through upi: sinhamit@icici or payment by bank
account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB
input() function in Category: Python by amit
🕙 Posted on 2023-06-27 at 17:36:02 Read in Hindi ...
A simple CLI app
In next few lessons, you will learn about basic programming skills. In previous pages, you have seen some data-types, and built-in functions. Let's build a command line or Python CLI executable program in step-by-step process, which will keep notes written by a person in a file. Save the following code in 0001.py file in your sub-(project) folder, namely python2023.
print( "First Program in Python: write something below" ) # user cannot interact with the OUTPUT of print() function
user = input()
# this line outputs terminal cursor, which is the next line of above OUTPUT because print() always returns a new line.
print( user ) # final result of the program (code)
Let's understand the above example. input()
is a built-in function, which not only display the string literal inside its parentheses, but also ask the user (public) to enter (input) something.
print( help( input
) ) # You should always run/
Help on built-in function input in module builtins:
input(prompt=None, /)
Read a string from standard input. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a
trailing newline before reading input.
If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.
None
Let's execute the above example, 0001.py in Python CLI (interactive shell). When you write the first line of code, the output is shown as below. Then, when you write the second line of code, that is, user = input(), the invisible cursor (or blinking command prompt) goes to next line, and waits for your input (response). In Mac/Linux, the Python CLI will be changed to >? when input()
function is executed.
C:\Users\yourName> python
Python 3.10.9 (tags/v3.10.9:1dd9be6, Dec 6 2022, 20:01:21) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print( "First Program in Python: write something below" )
First Program in Python: write something below
>>> user = input()
Always REMEMBER that when there is any input()
statement in your code, and you run/execute your program, it will ask you to input something, and waits for your response. When there are more input()
statement in your code, or there is any input()
statement in a while
or for
LOOP or inside any custom function, which can be called many times, then you have to response as many times, as the Python interpreter run/
>>> print( user )
After entering your response to input()
statement, for example hello, the above statement in Python CLI will output hello (if you write something else, then that will be the output). Since the response to input()
function is stored in variable name, user, the above line will output the data stored in the variable.
The input()
function always returns a string data-type which can be assigned to a variable name. If you enter any number, then it is also a string data-type, for example, '12'. If you write following statement in Python CLI, then the invisible cursor (or blinking command prompt) will wait, after outputting the string literal inside the parentheses.
>>> input( "Write something: " )
Write something:
If you don't place any white-(blank) space inside the pair of double quotes as shown herein above, the input()
function will output the string literal and the invisible cursor (or blinking command prompt) will be just close to that string value.
>>> exit()
C:\Users\yourName> exit
exit()
or quit()
is Python built-in function to quit the Python CLI, but exit is the MS-DOS CONSOLE command to quit the command shell or VSCode Editor Terminal. You can write the above example in another file, that is, 0002.py as follows and in next pages, it will be expanded further to make more interactive program.
user = input( "Write something: " )
print( user ) # outputs the final result of the program (code)
Leave a Comment:
Amit Sinha March 2nd, 2023 at 9:30 PM
😃 😄 😁 😆 😅 😂 😉 😊 😇 😍 😘 😚 😋 😜 😝 😶 😏 😒 😌 😔 😪 😷 😵 😎 😲 😳 😨 😰 😥 😢 😭 😱 😖 😣 😞 😓 😩 😫 😤
Ribhu March 3rd, 2023 at 9:30 PM
🐵 🐒 🐶 🐩 🐺 🐱 🐯 🐅 🐆 🐴 🐎 🐮 🐂 🐃 🐄 🐷 🐖 🐗 🐽 🐏 🐑 🐐 🐪 🐫 🐘 🐭 🐁 🐀 🐹 🐰 🐇