Learn Python - Bit by bit #3

in python •  7 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 >>

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!