Taking input in python

Like all high-level languages, Python is easy to read, takes less time to write, and is portable. This versatile programming language has two versions: For instance, Python 2 and Python 3.

Python 2 is no longer in development and all new features will be added in Python 3. 

Taking input in python

Python provides us with two inbuilt functions to read the input from the keyboard.

  • input ( prompt )
  • raw_input ( prompt )

Python 3.6 uses the input() method.

Python 2.7 uses the raw_input() method.

input() Function of python : In this paragraph, This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether user entered a string or a number or list. Moreover, If the input which a user provided and that input is not correct then python shows either syntax error or exception. For example –

name =input("Enter your name: ")

print(name)

OUTPUT

Enter your name: soluversity

soluversity

raw_input ( ) : This function works in older version (like Python 2.x). This function takes exactly what is typed from the keyboard, convert it to string and then return it to the variable in which we want to store. For example –

name =input(“Enter your name: “)

print name

OUTPUT

Enter your name: soluversity

soluversity

>>>

Python 3.6

username = input(“Enter username:”)
print(“Username is: ” + username)

OUTPUT

Enter username: soluversity

Username is: soluversity

Python 2.7

username = raw_input(“Enter username:”)
print(“Username is: ” + username)

OUTPUT

Enter username: soluversity

Username is: soluversity

>>>

Taking input from console in Python

What is Console in Python?

 Console (also called Shell) is basically a command line interpreter that takes input from the user i.e one command at a time and interprets it.A Python Console looks like this.

Taking input in python

Here we write command and to execute the command just press enter key and your command will be interpreted.

You are free to write the next command on the shell only when after executing the first command these prompts(>>>) have appeared. The Python Console accepts command in Python which you write after the prompt(>>>).

Taking input in python

Accepting Input from Console

User enters the values in the Console and that value is then used in the program as it was required.To take input from the user we make use of a built-in function input().

# INPUT

input1 =input()

# OUTPUT

print(input1)

Moreover,We can also type cast this input to integer, float or string by specifying the input() function inside the type. for instance

  1. Firstly, Typecasting the input to Integer: There might be conditions when you might require integer input from user/Console, the following code takes two input(integer/float) from console and typecasts them to integer then prints the sum.


INPUT
num1 =int(input())
num2 =int(input())

# printing the sum in integer
print(num1 +num2)

2)Secondly, Typecasting the input to Float: To convert the input to float the following code will work out.


INPUT
num1 =float(input())
num2 =float(input())

# printing the sum in integer
print(num1 +num2)

3) Lastly, Typecasting the input to String: All kind of input can be converted to string type whether they are float or integer. Therefore, We make use of keyword str for typecasting.

# INPUT

string =str(input())

# OUTPUT

print(string)

In conclusion, These are some basic methods of input function.

Please visit for more python tutorials: https://www.soluversity.in/how-to-learn-python-in-1-month

Source :https://www.python.org/

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *