Python User Input

Python supports user input which means the program pauses to ask the user for input. When the user enters some data and hits enter, Python stores the data in a variable as a string. We do this with the input() function.

Here is an example:

>>> hostname = input("Enter hostname: ")
Enter hostname: R1

Will show this output:

>>> print("Attempting to connect to hostname: %s" % hostname)
Attempting to connect to hostname: R1

The input() function can show a string to the user, for example, I am showing the prompt “Enter hostname: “. Otherwise, the user might not even notice he or she has to enter something.

Sign up for FREE to Continue Reading

Here's what you'll get:

  • Full Access to this lesson + 387 more.
  • Learn CCNA, CCNP and CCIE R&S. Explained as simple as possible.
  • Unlock Access to 424 lessons by becoming a member.
  • New Lessons Added Regularly. You'll Be the First to Know.
  • Content created by Rene Molenaar (CCIE #41726)

Takes 1 minute - No credit card needed


Forum Replies

  1. Hello,

    As far as I understand, the code shown in this example seems to be incorrect. There is no need for Print function here for the output we get when we execute the code. Please check. Thanks!

  2. Hello Rahul

    Hmm, I’m not sure what you mean. If the print function was not there, no output would appear after you put in the hostname. Results with the print function:

    https://cdn-forum.networklessons.com/uploads/default/original/2X/0/045683ed9b9e023a836a643de9db2b6a7051b14b.png

    Results without the print function:

    https://cdn-forum.networklessons.com/uploads/default/original/2X/b/b26cdeca77686c0f18f354eedbed794446c3dcc4.png

    Can you clarify the error that you are referring to?

    Thanks!

    Laz

Ask a question or join the discussion by visiting our Community Forum