Learn Python - Bit by bit #3

in #python6 years ago (edited)

python-logo-master-v3-TM.png
<< Learn Python - Bit by bit #2 - Variable

input()

So far we have been running a few instructions that always produce the same result. Which is kinda boring. So today we are focusing on input function. input() takes input from the user. Pretty obvious duh!

Go to repl.it and choose Python3 as the language. Inside main.py type:

msg = input()
print(msg)

Now click run.
On the console type "Hello World!" and press enter. You will notice that the same message will be printed back.

Now it's not pretty obvious to the user who doesn't have access to the code what to do because when you click run it doesn't show anything. So in order to avoid this problem let's show the user what he or she needs to do. input() allows to take an optional string (text) as an argument (something that goes inside ()) and when the function is run it displays the string we passed to it.

msg = input("Please enter a message: ")
print(msg)

2018-04-20_04-30-26.gif

We can even do something like this:

msg = input("Please enter a message: ")
print("Message: ",msg)

2018-04-20_04-33-47.gif

What we just did is we provided two strings to the print() function separated by a comma so that it displays both strings in the same order we have provided.

So that's it for today folks!

Learn Python - Bit by bit #4 - Data Types - Numbers >>

Coin Marketplace

STEEM 0.15
TRX 0.12
JST 0.025
BTC 55011.75
ETH 2337.88
USDT 1.00
SBD 2.34