Strings and Clear Output
str is short for string: a sequence of characters. Characters can be letters, spaces, digits, or symbols. Matching single or double quotes create text.
The quoted "250" is text even though its characters look like a number. The last line shows str and int. Quotes tell Python how to interpret what you wrote.
With numbers, + performs addition. With strings, + joins the text, called concatenation.
Outputs: 350, 100250, Hello, Alex, hahaha.
String multiplication by an integer repeats the text. Python does not silently guess how to add a number to a string: 100 + "250" raises TypeError.
An f-string is a string with an f just before the opening quote. Put a value or expression inside {...} to insert its result into the text.
Without f: Hello, {name}. With f: Hello, Alex.
The last line prints age 17. An f-string produces text; f is a prefix, not a separate variable you need to define.
A format instruction follows a colon inside the braces. :.2f means display a number with two digits after the decimal point.
Outputs: Price: HK$12.50, the type str, then 12.5 and float.
Formatting creates a readable message. It does not change the value or type of price. Try price = 12.567: the label displays HK$12.57 while the stored value remains 12.567.
Text from a form can look numeric. Conversion creates a value of a different type: use int() for whole-number text and float() for decimal text.
Outputs: 50.0 and 250 dollars. str() converts a value to text.
Choose the conversion to match the data: int("12.50") fails; float("12.50") works. int(3.9) gives 3, because it truncates toward zero rather than rounding.
A form returns the price and quantity as text. Convert the price with float() and the quantity with int(), then calculate the bill.
Check your result
Expected types: float, int. Expected bill: HK$50.00. Then change the inputs to "18.75" and "3"; the bill should be HK$56.25.
A method is an operation called after a value with a dot. name.upper() means “make an uppercase version of this text”. Parentheses call the method.
.strip() removes surrounding spaces; .upper() changes letter case. The combined result is HKUST CAFE.
These methods return new text. The original name still has its spaces and lowercase letters. .lower() makes a lowercase version.
Edit clean_name to remove surrounding spaces and convert the name to uppercase. Try a second input with different spaces and case.
Check your result
Use .strip() and .upper(). Expected output: Clean label: [HKUST CAFE], with no spaces next to the brackets. Test different surrounding spaces too.
Revenue is money from sales. Cost is money spent in this example. Profit = revenue − cost. Margin = profit ÷ revenue.
Profit is HK$370,000; margin is 29.6%. :, displays thousands commas. :.1% displays a fraction as a percentage with one decimal place.
We have not specified which expenses cost includes. The example does not establish a real company’s gross or net margin.
Prof. Xuhu Wan · HKUST ISOM · Introduction to Business Analytics