0% found this document useful (0 votes)
144 views1 page

Taking Input From User

The input() function allows a program to prompt a user to enter text from the keyboard. The entered text is returned as a string that can be assigned to a variable for later use or printing. If a different data type is needed, the input can be cast as an integer, float, or other type. It is important to handle exceptions in case invalid input is provided.

Uploaded by

Waqas Ashraf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views1 page

Taking Input From User

The input() function allows a program to prompt a user to enter text from the keyboard. The entered text is returned as a string that can be assigned to a variable for later use or printing. If a different data type is needed, the input can be cast as an integer, float, or other type. It is important to handle exceptions in case invalid input is provided.

Uploaded by

Waqas Ashraf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Taking Input from user

you can take input from the user using the built-in `input()` function. The `input()` function allows the
user to enter text, which will be read as a string by default. Here's a simple example:

# Taking input from the user and storing it in a variable

user_input = input("Please enter something: ")

# Displaying the input provided by the user

print("You entered:", user_input)

When this code is executed, it will prompt the user to enter something, and whatever the user types will
be stored in the `user_input` variable. The entered text will then be displayed back to the user using the
`print()` function.

Keep in mind that the `input()` function always returns a string. If you need to convert the input to a
specific data type (e.g., integer, float), you can use type casting like this:

# Taking input and converting it to an integer

age = int(input("Please enter your age: "))

# Taking input and converting it to a float

height = float(input("Please enter your height in meters: "))

Remember to handle exceptions if you expect the user to provide a specific type of input (e.g.,
numbers), as the user could enter something that cannot be converted to the desired data type.

You might also like