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

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?