Page 457 - ComputerScience_Class_11
P. 457
Program 4: To demonstrate conversion of string input to numeric types using int() and float() for mathematical
operations.
Program 4.py
File Edit Format Run Options Window Help
# Method 1: Convert after input
age_str = input("Enter your age: ") Output
age = int(age_str) # Convert string to integer
Enter your age: 16
print("In five years, you will be", age + 5)
In five years, you will be 21
# Method 2: Convert during input
height = float(input("Enter height in cm: ")) Enter height in cm: 170.5
print("height in meters is", height / 100) height in meters is 1.705
In the second method, the program works from the inside out. First, input() takes the value entered by the user as
a string. This value is then immediately passed to float(), which converts it into a decimal number. The converted
value is finally stored in the variable height.
12.2 DISPLAYING OUTPUT WITH PRINT()
In Python, the print() function is used for displaying information. It is commonly used for showing results of
computations, user messages or debugging data. Understanding how to use the print() function effectively is
essential for interacting with users and for testing your code.
12.2.1 Syntax of the print() Function
The basic syntax of the print() function is as follows:
print(*objects, sep=' ', end='\n')
• *objects: The values or variables you want to print. Multiple values separated by comma can be passed and they
will be printed in the order given.
• sep: Defines what will separate the printed objects. The default separator is a space ' '. It can be changed to any
other text or symbols.
• end: Specifies what to print at the end. By default, it’s a newline (\n), but you can change it to something else.
12.2.2 Key Attributes of the print() Function
1. objects:
The values you want to display can be strings, numbers, or variables. Strings must be written inside quotes, while
variables are written without quotes.
Example:
Program.py
File Edit Format Run Options Window Help
f_name="Yogesh"
Output
l_name="Parihar"
print("Hello", f_name, l_name) Hello Yogesh Parihar
2. sep:
This attribute determines the separator used between the objects. The default is a space ' ', but you can specify any
different value.
Data Processing in Python 455

