google.com, pub-8786015629279405, DIRECT, f08c47fec0942fa0 Input Function/Statement in Python

Input Function/Statement in Python

0

Python offers an input method that allows you to request text input from a user. This function is used to inform the programme to come to a halt and wait for the user to enter the data. Raw input() is a built-in function in Python 2, whereas input() is a built-in function in Python 3. ().

 

Input Function in Python

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

 

1.     input ( prompt )

2.   raw_input ( prompt )

 

1. input (): This function takes the user's input and then evaluates the expression, which means Python recognizes if the user submitted a string, a number, or a list automatically. Python will raise a syntax error or an exception if the input provided is incorrect. 

For Example:

 

name = input("Enter your Name: ")

print(name)

 

Output is:

Enter your Name: Ram

Ram


 

2. raw input (): This function is still functional in previous versions (like Python 2.x). This function takes what is typed from the keyboard, converts it to a string, and then returns it to the variable we want to keep it in.

For Example:

 

name = raw_input("Enter your Name: ")

print(name)

 

Output is:

Enter your Name: Ram

Ram


 

In Python, here's how the input function works:

  • When the input() & raw_input function is called, the programme execution is halted until the user provides an input.
  • The text or message that appears on the output screen to prompt the user to submit an input value is optional, i.e. the prompt that appears on the screen is not required.
  • The input function converts whatever you type into a string. Even if an integer value is entered, the input() function converts it to a string.
  • You must use typecasting to explicitly convert it to an integer in your code.

·

Typecasting in Python:

Typecasting is the process of converting one data type into another. When you need to convert a data type, there are a few scenarios.

For example, suppose you wish to multiply two values, one of which is an integer and the other a string. Then, before adding, you must use Python typecasting to transform the string data type to an integer.

There are a few procedures that are used for type casting in Python:

 

int() is a function that specifies an integer literal. Â From a text literal and a Float, create an integer number.

str() specifies a string literal. Make a string type out of an integer or float literal.

float() specifies the float data types. From an integer and a string literal, create a float literal.

 

Now, we can also use typecasting with the input() function to typecast this input to integer, float or string.

1. Typecast the input() function for integer:

There may be times when you need integer input from the user or the console; the following code takes two inputs (integer/float) from the console and converts them to integers.

 

# Write a Program to add Two Number Using Python

 

num1 = int(input(“Enter Your First Number”))

num2 = int(input(“Enter Your Second Number”))

 

print(“The Sum of Two Number is =”, num1 + num2)

 

Output is:

Enter Your First Number 23

Enter Your Second Number 23

The Sum of Two Number is =46

 

 

2. Typecast the input() function for Float:

There may be times when you need float input from the user or the console; the following code takes two inputs (integer/float) from the console and converts them to integers.

 

# Write a Program to add Two Number Using Python

 

num1 =float(input(“Enter Your First Number”))

num2 = float(input(“Enter Your Second Number”))

 

print(“The Sum of Two Number is =”, num1 + num2)

 

 

Output is:

Enter Your First Number 23

Enter Your Second Number 23.00

The Sum of Two Number is =46.00

 

 

3. Typecast the input() function for String:

 

Any form of input, whether float or integer, can be converted to string. For typecasting, we utilise the term str.

 

# Write a Program to Enter a String Value

 

string = str(input(“Enter Any Type of Value ”))

 

# output

print(string)

 

Output is :

Enter Any Type of Value Ram

Ram

 


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Thank you for your interest 😊

We will back shortly after reviewing...

Thank you for your interest 😊

We will back shortly after reviewing...

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top