input() in Python

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 20:- input() in Python

In Python, input() is a built-in function used to read input from the user via the console or terminal. The input() function allows you to prompt the user for information, and the user can respond by typing in a value, which is then returned as a string.

The syntax of the input() function is as follows:

pythonCopy code

input([prompt])

Here's what the argument means:

  • prompt (optional): An optional string that specifies the message to be displayed to the user before waiting for input. This message is often used as a prompt to instruct the user on what input is expected.

Example:

pythonCopy code

name = input("Enter your name: ") print("Hello, " + name + "! Welcome.")

In this example, the program will display the message "Enter your name: " to the user in the console. The user can then type in their name and press Enter. The input() function will read the input as a string and assign it to the variable name. The program will then print a greeting message using the entered name.

Keep in mind that the input() function always returns a string, even if the user enters a number. If you need the user input as a specific data type (e.g., integer, float), you'll need to perform type conversion using functions like int() or float().

Example:

pythonCopy code

age_str = input("Enter your age: ") age_int = int(age_str)  # Convert the input string to an integer print("In 10 years, you will be " + str(age_int + 10) + " years old.")

In this example, the user enters their age as input. The input is initially stored as a string in the variable age_str. We then use int(age_str) to convert the input string to an integer, so we can perform numerical calculations on it. The program then adds 10 to the user's age and prints the result as a string.

The input() function is commonly used for creating interactive programs and taking user input for various purposes, such as customizing program behavior, collecting user data, and more.

3. Input And Output In Python

0 Comments

Start the conversation!

Be the first to share your thoughts

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support