-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathAdd input
More file actions
58 lines (44 loc) · 1.81 KB
/
Add input
File metadata and controls
58 lines (44 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Explanation of str(input()), int(input()), and float(input())
This document explains how to use `str(input())`, `int(input())`, and `float(input())` in Python with simple examples.
---
## **1. `str(input())`**
- **Explanation:**
- `input()` collects data from the user as a string by default.
- Wrapping it in `str()` ensures the input is treated as a string explicitly.
- Useful when working with text.
**Example:**
```python
name = str(input("Enter your name: ")) # User enters: Obay
print("Hello, " + name) # Output: Hello, Obay
```
---
## **2. `int(input())`**
- **Explanation:**
- `input()` collects data as a string, but `int()` converts it into an integer.
- Use this for numbers that don’t have decimals (whole numbers).
- If the user enters a non-numeric value (like "abc"), it will throw an error.
**Example:**
```python
age = int(input("Enter your age: ")) # User enters: 25
print("You are " + str(age) + " years old.") # Output: You are 25 years old.
```
---
## **3. `float(input())`**
- **Explanation:**
- `input()` collects data as a string, but `float()` converts it into a decimal (floating-point number).
- Use this for numbers that might include decimals.
**Example:**
```python
price = float(input("Enter the price: ")) # User enters: 12.99
print("The price is $" + str(price)) # Output: The price is $12.99
```
---
## **Key Notes**
1. Always use the correct type (`int` for whole numbers, `float` for decimals, and `str` for text).
2. If the input can't be converted (e.g., entering text when `int()` is expected), Python will show an error.
3. You can also use `type()` to verify the data type of user input.
**Example:**
```python
value = int(input("Enter a number: "))
print("Type:", type(value)) # Output: <class 'int'>
```