8.Getting user input

8.Getting user input

Python provides the input () method to retrieve user input. As the name implies, it reads all the characters you type in the environment until you hit the enter key. Consider the following application:

 1 name   = input("Enter your name: ")                              
 2 age    = input("Enter your age: ")      
 3 height = input("Enter your height: ")
 4                                               
 5 #Print a blank line                          
 6 print()                          
 7                                               
 8 #Show the details you typed                  
 9 print("Name is {0}.".format(name))    
10 print("Age is {0}.".format(age))        
11 print("Height is {0}.".format(height))

Result:

Enter your name: John
Enter your age: 18
Enter your height: 160.5

Name is John.
Age is 18.
Height is 160.5.

We first define 3 variables to store data in the program (lines 1, 2 and 3). The application asks the user to enter his name (line 1). In line 2 you enter your name as a user.

Then the program asks us age (line 3). In line 6 we also create a dash by the print () method to create a gap between your inputs and outputs.

Now run the program and see the result by entering the desired values.

To view or add a comment, sign in

More articles by Mohammad Moradi (He/Him)

  • 7.Phrases and Operators

    Learn two words first: Operator: are symbols that perform specific actions. Operand: The values that the operators…

  • 6.Convert data types

    In Python, it is possible to convert one type to another, so-called Type Casting. Python has a set of predefined…

  • 5.Data types

    The types of data available in Python are: Using variables: Unlike languages like Java and C #, where we had to specify…

  • 4.Control characters

    Control characters are composite characters that begin with a backslash (\) followed by a letter or number and…

  • 3.Build a simple application

    Let's write a very simple program in Python. This program displays a message.

  • 2.Download and install Python 3.8

    There are various development environments or IDEs for programming in different languages that help programmers write…

  • 1.What is Python

    Python is a versatile, object-oriented and open source programming language designed by Guido van Rossum in 1991 in the…

  • the mark @ in C#

    The @ mark allows you to override the control characters and create a more natural, readable string. When using control…

  • 31.Class as field

    Like all the variables we have used so far, one class or structure can be converted to another class member variable…

  • 30.Nested class

    Sometimes it is necessary to define a class in another class. The most important reason for this is to limit the scope…

Explore content categories