Home » Python

input() function with example in Python

Python input() function: Here, we are going to learn about the input() function in Python with example.
Submitted by IncludeHelp, on April 03, 2019

Python input() function

input() function is a library function, it is used to get the user input, it shows given message on the console and wait for the input and returns input value in string format.

Syntax:

    input(prompt)

Parameter(s): promot – a string value/message that will display on the console.

Return value: str – it returns user input in string format.

Example:

    Input:
    text = input("Input any text: ")
    
    # if user input is "Hello world!"
    print("The value is: ", text)
    
    Output:
    Input any text: Hello world!

Example 1: Input a string and print it and its type

# python code to demonstrate example
# of input() function

# user input
text = input("Input any text: ")

# printing value 
print("The value is: ", text)

# printing type
print("return type is: ", type(text))

Output

Input any text: Hello world!
The value is:  Hello world!
return type is:  <class 'str'>

Example 2: Input different of types of values and print them along with their types

# python code to demonstrate example
# of input() function

# input 1
input1 = input("Enter input 1: ")
print("type of input1 : ", type(input1))
print("value of input1: ", input1)

# input 2
input2 = input("Enter input 2: ")
print("type of input2 : ", type(input2))
print("value of input2: ", input2)

# input 3
input3 = input("Enter input 3: ")
print("type of input3 : ", type(input3))
print("value of input3: ", input3)

Output

Enter input 1: 12345
type of input1 :  <class 'str'>
value of input1:  12345
Enter input 2: 123.45
type of input2 :  <class 'str'>
value of input2:  123.45
Enter input 3: Hello [email protected]#$
type of input3 :  <class 'str'>
value of input3:  Hello [email protected]#$

Input integer and float values

There is no direct function that can be used to take input in either in an integer value or in a float value.

input() function takes an input from the user of any type like integer, float, string but returns all values in string format. If we need values in an integer or float types, we need to convert them.

To convert input in an integer, we use int() function.

To convert input in a float, we use float() function.

Example 3: Python code to input integer and float values

# python code to demonstrate example
# of input() function

# input an integer value
num = int(input("Enter an integer value: "))
print("type of num: ", type(num))
print("value of num: ", num)

# input a float value
num = float(input("Enter a float value: "))
print("type of num: ", type(num))
print("value of num: ", num)

Output

Enter an integer value: 12345
type of num:  <class 'int'>
value of num:  12345
Enter a float value: 123.45
type of num:  <class 'float'>
value of num:  123.45





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.