Chapter 0 · Section 2

Strings and Clear Output

Prof. Xuhu Wan

§2 — Strings and Clear Output

Strings and Clear Output

str: A String Is Text

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.

The Same + Has Different Jobs

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.

f-strings: Put Values into a Message

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.

Formatting: Two Decimal Places

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.

Convert Text Before Doing Arithmetic

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.

Practice: Convert Text into Numbers

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.

Strings: Working with Text

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.

Practice: Clean a Company Label

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.

A Shop Calculation: Revenue, Cost, Profit

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.