User Input in Python and Type Casting for Beginners
User input and type casting are two important ideas that you need to understand early on when you start learning Python.
These help you make your programs interactive, dynamic, and realistic, just like apps in the real world, where users enter data and the program reacts in a certain way.
User Input in Python
What does the input() function do in Python?
To put it simply, input() is a command that lets your program get information from the user.
Python sees what you type as a string when the program is running.
For example:
In this case, input() takes the name you typed, saves it in the name variable, and prints it.
________________________________________________________________________________________________________________
Why do we need user input?
Think about making:
An app for a calculator
A form to sign up online
A calculator for bills or EMIs
In each of these situations, the program needs to get information from the user, such as numbers, names, or prices.
Your code can’t respond to real-world data unless a user gives it input.
Example (without user input):
No matter how many times you run this, the result is always 30.
But with user input:
Entering 10 and 20 will result in 1020, not 30.
The reason for this is that input is always a string. Let’s examine that next.
The input’s default data type is
Even if you type numbers, Python treats everything you enter using input() as a string data type.
Output:
Therefore, Python interprets 25 as ’25’ (string) even if you type it.
Why Does “10 + 20” Turn Into “1020”?
Python joins strings rather than adding them numerically. Concatenation is the term for this process.
Example:
Input:
Output:
Since “10” + “20” = “1020,” both are strings.
In order to correct this, input values must be converted into the appropriate type, which is where type casting is useful.
Display a Message in input()
A message telling the user what to type can be added inside input().
Prompt messages are always used by good programs to help users.
Getting Numbers as Inputs
You need to change the string to an integer or a float in order to do math.
Now, if you put in 10 and 20, you get:
Example from Real Life: EMI Calculator
Suppose you wish to figure out a fixed 6-month EMI of ₹600.
If you enter 6, the result will be:
Interactive programs operate like this.
Practice Exercises
Try writing:
A basic interest calculator that accepts user input
A tiny calculator with addition, subtraction, multiplication, and division functions
Determine the user’s age by asking for their birth year.
Python Type Casting
Knowing that user input always results in a string, type casting enables you to change data types as necessary.
Type casting: what is it?
Type casting is the process of converting one data type to another.
For example:
Convert “25” from a string to an integer.
Float 2.5 to integer 2 conversion
Convert the number 5 to the string “5”
We take this action to enable Python to execute proper operations.
The Reasons for the Need for Type Casting
Suppose you are developing a shopping cart.
The user types in:
Both are strings by default.
Python will display an error or produce the incorrect result if you enter price * quantity.
Thus, you must convert them:
Output:
Regular Type Casting Operations
| Function | Description |
|---|---|
int() |
Converts data into an integer |
float() |
Converts data into a float |
str() |
Converts data into a string |
bool() |
Converts data into Boolean (True/False) |
1. int() – Convert to Integer
Example 1: Float to Int
Decimal part gets removed.
Example 2: Bool to Int
Example 3: String to Int
An error will be displayed if the string contains alphabets like “Python.”
2. float() – Convert to Float
Example 1: Int to Float
Example 2: Bool to Float
Example 3: String to Float
An error is displayed if you attempt to use int(“123.5”).
However, you can:
3. str() – Convert to String
Any data type can be safely converted to a string using str().
When displaying messages with numbers, this is helpful.
Output:
4. bool() – Convert to Boolean
Boolean means True or False.
Python treats:
-
0, 0.0, “”, ”, None →
False -
Any string that is not empty or has a value of zero → True
Examples:
Important:0 (integer) is False"0" (string) is True — because it’s not empty.
Applications of Type Casting in Real Life
User input is converted to an integer for computations.
Converting string data from a file to numeric values
Verifying True/False fields on switches or login forms
Example:
Quick Summary
| Function | Converts From | Converts To | Example |
|---|---|---|---|
| int() | String/Float | Integer | int(“10”) → 10 |
| float() | String/Int | Float | float(“3.5”) → 3.5 |
| str() | Any | String | str(25) → “25” |
| bool() | Any | Boolean | bool(“”) → False |
Resource:
Official Python Input Documentation
W3Schools Python Input Tutorial
Further learning
Python Data Types for Beginners: A Simple Guide to 4 Key Types – int, float, str, and bool
Activities for Practice
- Create a program that uses user input (length, width) to determine a rectangle’s area.
- Create a program that prints the average of three subjects’ grades.
- Determine and show the user’s age after asking for their name and birth year.
- Make a check for the login that yields. If the password and username are both non-empty strings, then the statement is accurate.
Conclusion:
The fusion of data science in the finance sector is not just a technological evolution but also a fundamental shift in the way the financial industry operates. From predictive analytics to personalized financial services, the applications of data science are reshaping traditional practices and opening up new possibilities. As we all move forward, the synergy between finance and data science will continue to evolve, creating a more robust, efficient, and resilient financial ecosystem. In this data-driven era, those who embrace the power of data science will be at the forefront of innovations and success in the world of finance.
Want to know what else can be done by Data Science?
If you wish to learn more about data science or want to advance your career in the data science field, feel free to join our free workshop on Master’s in Data Science with Power BI, where you will get to know how exactly the data science field works and why companies are ready to pay handsome salaries in this field.
In this workshop, you will get to know each tool and technology from scratch, which will make you skillfully eligible for any data science profile.
To join this workshop, register yourself on ConsoleFlare, and we will call you back.
Thinking, Why Console Flare?
Recently, ConsoleFlare has been recognized as one of the Top 10 Most Promising Data Science Training Institutes of 2023.
Console Flare offers the opportunity to learn Data Science in Hindi, just like how you speak daily.
Console Flare believes in the idea of “What to learn and what not to learn,” and this can be seen in their curriculum structure. They have designed their program based on what you need to learn for data science and nothing else.
Want more reasons?
Register yourself on ConsoleFlare, and we will call you back.
Log in or sign up to view
See posts, photos, and more on Facebook.

