Table of contents
1.
Introduction
2.
Using the input() Function
2.1.
Basic Syntax
2.2.
Example: Taking String Input
2.3.
Python
2.4.
Example: Taking Integer Input
2.5.
Python
2.6.
Example: Taking Float Input
2.7.
Python
3.
Prompting the User for Input
3.1.
Python
4.
Type Conversion for User Input
4.1.
Converting String to Integer
4.2.
Converting String to Float
5.
Example of Type Conversion
5.1.
Python
6.
Frequently Asked Questions
6.1.
How do I take input from a user in Python?
6.2.
How can I take multiple inputs in one line?
6.3.
What if the user enters invalid input?
7.
Conclusion
Last Updated: Jul 20, 2024
Easy

Taking Input in Python

Author Ravi Khorwal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Taking input from users is a fundamental part of programming. It allows programs to interact with users, making them more dynamic and versatile. In Python, getting input from users is simple and efficient. 

Taking Input in Python

This article will guide you through the various ways to take input in Python, covering basic syntax, examples, and some common practices.

Using the input() Function

Basic Syntax

The input() function is the primary way to get input from users in Python. It reads a line from the input (usually from the keyboard) and returns it as a string.

user_input = input()

Example: Taking String Input

Let's take a simple example where we ask the user to enter their name and then print it.

python

  • Python

Python

name = input("Enter your name: ")

print("Hello, " + name + "!")
You can also try this code with Online Python Compiler
Run Code


Output

Enter your name: Alice
Hello, Alice!

Example: Taking Integer Input

To take an integer input, you need to convert the string input to an integer using the int() function.

python

  • Python

Python

age = int(input("Enter your age: "))

print("You are " + str(age) + " years old.")
You can also try this code with Online Python Compiler
Run Code


Output

Enter your age: 20
You are 20 years old.

Example: Taking Float Input

Similarly, for a float input, use the float() function.

  • Python

Python

height = float(input("Enter your height in meters: "))

print("Your height is " + str(height) + " meters.")
You can also try this code with Online Python Compiler
Run Code


Output

Enter your height in meters: 1.75
Your height is 1.75 meters.

Prompting the User for Input

Adding a prompt message makes it clear what the user is expected to enter.

  • Python

Python

name = input("What is your name? ")

print("Nice to meet you, " + name + "!")
You can also try this code with Online Python Compiler
Run Code


Output

What is your name? Bob
Nice to meet you, Bob!

Type Conversion for User Input

When taking input, it's often necessary to convert the input from a string to another data type.

Converting String to Integer

number = int(input("Enter a number: "))
print("You entered the number " + str(number))

Converting String to Float

decimal_number = float(input("Enter a decimal number: "))
print("You entered the number " + str(decimal_number))

Example of Type Conversion

  • Python

Python

num1 = int(input("Enter the first number: "))

num2 = int(input("Enter the second number: "))

sum = num1 + num2

print("The sum of the two numbers is " + str(sum))
You can also try this code with Online Python Compiler
Run Code


Output

Enter the first number: 10
Enter the second number: 20
The sum of the two numbers is 30

Frequently Asked Questions

How do I take input from a user in Python?

Use the input() function to get input from the user.

How can I take multiple inputs in one line?

Use the split() method to split the input string into multiple parts.

What if the user enters invalid input?

Use try-except blocks to handle invalid input gracefully.

Conclusion

Taking input in Python is straightforward with the input() function. Whether you are taking single or multiple inputs, converting data types, or validating input, Python provides simple and efficient methods to handle user input. By following best practices and validating input, you can create interactive and user-friendly programs. Keep practicing these techniques to master user input handling in Python.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass