Lecture
Using the input Function to Receive User Input
The input() function is used to receive input from the user, and it always returns the input data as a string.
Using the input() Function
# Receive input from the user user_input = input("Please enter your name: ") # Output the entered data print(f"Welcome, {user_input}!")
Converting the Data Type of Input
Since the data from input() is returned as a string, you may need to convert it to another data type for arithmetic operations or logical comparisons.
For instance, if you want to receive a number from the user for mathematical operations, the input needs to be converted to an integer or float.
In Python, use int() to convert a string to an integer, and float() to convert it to a floating-point number.
The example below shows how to convert user input to an integer using the int() function.
Data Type Conversion of Input
age_input = input("Please enter your age: ") age = int(age_input) # Convert string to integer using int() print(f"You are {age} years old.")
Previous lessonComparison of format() Function and f-StringsNext lessonIndicating Invalid Values with ValueError
Lessons in this chapter · String Formatting Made Easy
- 1. Inserting Variables into Strings with the format() Function
- 2. Tips for Using `format()` Function - indexError
- 3. Displaying Integers with the format() Function
- 4. Handling Floating-Point Numbers with the format() Function
- 5. Multiple-choice quiz
- 6. Case Conversion Methods upper() and lower()
- 7. Removing Leading and Trailing Whitespace with the strip() Function
- 8. String Composition Verification with isOOO() Methods
- 9. Finding the Position of a Specific Character Using find() and rfind()
- 10. Using the split() Method for Strings
- 11. How to Check if a Specific Character Exists in a String
- 12. Multiple-choice quiz
- 13. How to Format Strings with f-Strings
- 14. Comparison of format() Function and f-Strings
- 15. Using the `input` Function to Receive User Input
- 16. Indicating Invalid Values with ValueError
- 17. Coding Quiz - Splitting Strings
- 18. Multiple-choice quiz
- 19. Fill-in-the-blank quiz
Quiz
0 / 1
What is the return type of the input() function always?
Integer
Float
String
Boolean
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help