Python is a powerful and beginner-friendly programming language. If you are just starting out, you might have come across the terms input
and type casting
. These concepts are fundamental to making your programs interactive and functional. In this blog, we will learn how to take input from users and how to make that data in an appropriate format or type using type casting.
Table of Contents
What is the Input function?
When you want to take input from the user, you simply write input()
and run it. Python will then prompt you to enter something. For example, if I type my name, Python will capture that input. But where does it store this information? To store the input, we can use a variable. Let’s say I create a variable called name
and assign it the value from the input function. So, I can write:
name = input()
PythonWhen you run this, Python will pause and wait for you to input your name, which it then stores in the name
variable.
Making Input Clear
But how does the user know what to enter? This is where we can add a message inside the input function. For example:
name = input("What is your name? ")
print("@", name)
PythonNow, when I run this code, it will clearly ask me, “What is your name?” This makes it easier for the user to understand what is expected.
What is your name? Inkita
@ Inkita
BashCheck Data Types
Now, let’s talk about data types. When we take input, Python treats everything as a string by default. For example, if I enter a number, it is still considered a string. To check the type of the input, we can use the type()
function.
age = input("Enter your age: ")
print(age, type(age))
PythonThis will show that the type of age
is a string, even I entered a number.
Enter your age: 12
12 <class 'str'>
BashType Casting
When you use the input()
function, all data is stored as a string by default. So, what if I want to use that age
as a number? This is where type casting comes into play. Type casting allows us to convert one data type into another.
There are two types of typecasting:
- Implicit typecasting
- Explicit typecasting
1. Implicit typecasting
It automatically converts one data type to another when needed. It is automatically performed by the compiler or interpreter. For example:
x=5 # Integer
y=2.5 # Float
z=x+y # X is implicitly converted to float
print(z) # Output: 7.5 (float data type)
PythonTypecasting
Explicit type casting is when you convert a value from one type to another using functions. Python provides several built-in functions for type casting, so when I need use age
as a number I will change it’s type.
1. int()
Integer.
Converts data to an integer. Example
age = int(input("Enter your age: "))
print(age, type(age))
# Ouptut:
# Enter yor age: 12
# 12 <class 'int'>
Python2. float()
Float Number
Converts data to a floating-point number. Example
pi = float(input("Enter pi value: "))
print(age, type(age))
# Ouptut:
# Enter pi value: 3.14
# 3.14 <class 'float'>
Python3. str()
String
Converts data to a string. Example
num = 12
string = str(num)
print(string, type(string))
# Ouptut:
# 12 <class 'str'>
Python4. bool()
Boolean
Converts data to a boolean (True/False). Example
is_hungry = bool(input("Are you hungry? (yes/no): "))
print(is_hungry, type(is_hungry))
# Ouptut:
# Are you hungry? (yes/no): yes
# True <class 'bool'>
PythonBut here is something wrong.
Are you hungry? (yes/no): no
True <class 'bool'>
BashEven if you enter no
still it will give you True
. But the question is why? In Python, the following values are treated as False
when evaluated as a Boolean.
None
- Numeric Zero of Any Type:
0
,0.0
,0j
''
(empty string)[]
(empty list)()
(empty tuple){}
(empty dictionary)
Hence only above value inside bool()
function gives False
.
Advanced Type Casting Examples
- Converting to List
name = "Bulbul"
name_list = list(name)
print(name_list)
# Outputs: ['B', 'u', 'l', 'b', 'u', 'l']
Python- Converting to Set:
unique_chars = set(name)
print(unique_chars)
# Outputs: {'B', 'u', 'l'}
Python- Working with Complex Numbers:
complex_number = complex(4, 5)
print(complex_number)
# Outputs: (4+5j)
PythonSummary
So we have learned how the input()
function is used to gather user input, which is always stored as a string by default. To handle inputs as other data types, explicit type casting using functions like int()
, float()
, and str()
is demonstrated, along with practical examples.
The blog also covers implicit type casting, where Python automatically converts data types during operations. Advanced examples, including conversions to lists, sets, and complex numbers, are provided to showcase the versatility of type casting.