Skip to content
Module 02 of 1240 min readBeginner

Strings, numbers, and formatting

f-strings, integer vs float, string methods, and the formatting tricks that make output readable.

17%

Listen along

Read “Strings, numbers, and formatting” aloud

Plays in your browser using on-device text-to-speech — nothing leaves the page.

Learning objectives

By the end of this module, you should be able to:

  • 01Compose f-strings with format specifiers for percentages, thousands separators, and currency
  • 02Use string methods (upper, lower, replace, split, strip) confidently for everyday data cleaning
  • 03Distinguish / from // and understand float arithmetic precision issues
  • 04Apply math.isclose() for safe float comparisons instead of ==

Strings, numbers, and the formatting between them are the bread and butter of any analysis pipeline. Get fluent at f-strings, integer-vs-float arithmetic, and basic string methods, and you can move data around with confidence.

Strings — the basics

Strings can use single or double quotes interchangeably. Triple quotes (''' or """) allow multi-line strings. Strings are immutable: methods return new strings rather than modifying in place.

python
name = 'Kenya'
name.upper() # 'KENYA' — returns a new string
name.lower() # 'kenya'
name.replace('K', 'B') # 'Benya'
len(name) # 5
'Africa' in 'sub-Saharan Africa' # True

f-strings — the formatting tool you'll use most

Python 3.6+ introduced f-strings, which allow you to embed expressions inside string literals using curly braces. They are by far the cleanest formatting tool in the language.

python
rate = 0.1235
print(f'The CBR is {rate:.2%}') # 'The CBR is 12.35%'
print(f'KES {1234567:,.0f}') # 'KES 1,234,567'
name, gdp = 'Kenya', 110_000
print(f'{name}: ${gdp:,}M') # 'Kenya: $110,000M'

Integer vs float division

In Python 3, the / operator always returns a float, even for integer operands. Use // for integer (floor) division when you want an int result.

python
10 / 3 # 3.3333333333333335 (float)
10 // 3 # 3 (int)
10 % 3 # 1 (modulo)
10 ** 3 # 1000 (exponentiation)

Numbers — three things to know

  • Integer overflow doesn't exist in Python — ints grow as needed.
  • Floats are IEEE 754, with the usual precision warnings: 0.1 + 0.2 == 0.3 is False because 0.1 has no exact binary representation.
  • Use the decimal module for money or any context where 0.1 + 0.2 == 0.3 must be True.

Float comparison is dangerous

Never compare floats with ==. Use math.isclose(a, b) or abs(a - b) < tolerance. The 0.1 + 0.2 == 0.3 trap catches every junior analyst at least once.

Exercise

Given rate = 0.0875, format it as a percentage with two decimal places using an f-string.

Key takeaways

  • f-strings (PEP 498) are the cleanest formatting tool — use them by default
  • 0.1 + 0.2 == 0.3 is False in IEEE 754; never compare floats with ==
  • For money or any context requiring exact decimal arithmetic, use the decimal module
  • Python 3's / is always float division; // is integer (floor) division

Further reading

  1. 01
  2. 02

    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    David Goldberg · ACM Computing Surveys · 1991

  3. 03
Loading progress…
LeadAfrikPublic Economics Hub