Convert carefully
Input is text. Convert only after deciding which values are valid.
Conversion, math and string functions
Unit 4 · Python’s ready-made toolkit
A built-in function is available without importing a module. It accepts inputs (arguments), follows a defined rule, and returns a result. String operations such as strip() are methods called on a string; len() is a built-in function that accepts a string. Select an entry to load an editable example into the live runner.
Input is text. Convert only after deciding which values are valid.
sum(), min(), max(), and len() answer different questions.
Methods return a new string. Save the result to keep the transformation.
Browse all 45 names listed in the course plan: 12 conversion, 7 numeric, and 26 string operations.
Conversion · built-in function
Convert a valid numeric value to an integer.
Expected result
42 · intRemember: int(3.9) truncates toward zero; it does not round.
value = int("42")
print(value)
print(type(value).__name__)Output
Your program's output will appear here.
NameError: name 'mark' is not defined
NameError: A name was used before Python could find a variable or function with that name.
IndexError: list index out of range
IndexError: The requested position is outside the items in this list or sequence.
KeyError: 'email'
KeyError: That key does not exist in this dictionary.
TypeError: can only concatenate str (not "int") to str
TypeError: This operation does not work with the kind of value it received.
ZeroDivisionError: division by zero
ZeroDivisionError: A number cannot be divided by zero.
SyntaxError: invalid syntax
SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.
IndentationError: expected an indented block
IndentationError: The spaces at the start of a line do not match Python's block structure.
Worked patterns
Change the data and predict the output before running each example.
Conversion may fail for invalid text. Production code should validate or handle ValueError.
raw_marks = "91" marks = int(raw_marks) print(marks + 4) print(type(marks).__name__)
Output
Your program's output will appear here.
NameError: name 'mark' is not defined
NameError: A name was used before Python could find a variable or function with that name.
IndexError: list index out of range
IndexError: The requested position is outside the items in this list or sequence.
KeyError: 'email'
KeyError: That key does not exist in this dictionary.
TypeError: can only concatenate str (not "int") to str
TypeError: This operation does not work with the kind of value it received.
ZeroDivisionError: division by zero
ZeroDivisionError: A number cannot be divided by zero.
SyntaxError: invalid syntax
SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.
IndentationError: expected an indented block
IndentationError: The spaces at the start of a line do not match Python's block structure.
sum() gives a total, len() gives a count, and round() creates a useful display value.
marks = [82, 91, 76]
average = sum(marks) / len(marks)
print("Total:", sum(marks))
print("Highest:", max(marks))
print("Average:", round(average, 1))Output
Your program's output will appear here.
NameError: name 'mark' is not defined
NameError: A name was used before Python could find a variable or function with that name.
IndexError: list index out of range
IndexError: The requested position is outside the items in this list or sequence.
KeyError: 'email'
KeyError: That key does not exist in this dictionary.
TypeError: can only concatenate str (not "int") to str
TypeError: This operation does not work with the kind of value it received.
ZeroDivisionError: division by zero
ZeroDivisionError: A number cannot be divided by zero.
SyntaxError: invalid syntax
SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.
IndentationError: expected an indented block
IndentationError: The spaces at the start of a line do not match Python's block structure.
strip() removes edge spaces, title() changes capitalization, and startswith() checks the cleaned result.
student = " mira patel "
clean_name = student.strip().title()
print(clean_name)
print("Length:", len(clean_name))
print("Starts with Mira:", clean_name.startswith("Mira"))Output
Your program's output will appear here.
NameError: name 'mark' is not defined
NameError: A name was used before Python could find a variable or function with that name.
IndexError: list index out of range
IndexError: The requested position is outside the items in this list or sequence.
KeyError: 'email'
KeyError: That key does not exist in this dictionary.
TypeError: can only concatenate str (not "int") to str
TypeError: This operation does not work with the kind of value it received.
ZeroDivisionError: division by zero
ZeroDivisionError: A number cannot be divided by zero.
SyntaxError: invalid syntax
SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.
IndentationError: expected an indented block
IndentationError: The spaces at the start of a line do not match Python's block structure.
sum(["82", "91"])These are strings, not numbers. Convert validated numeric text before arithmetic.
int("nine")Python cannot parse this as an integer. Validate input and explain the expected format.
name.strip() # result ignoredStrings are immutable. Assign the result: name = name.strip().
"room 2".isalpha()False: isalpha requires every character to be a letter.
1. What does int(3.9) produce?
2. Which expression counts items in marks?
3. What is " Mira ".strip()?
4. What does "banana".find("z") return?
Choose functions by asking three questions
What type is the data now? What result do I need? What should happen for empty or invalid data? For example, guard an average against an empty list before dividing by len(marks). Knowing edge cases is part of using a function well.