Phase 02 · Python FoundationsLesson 2 of 8
Data Types, Strings & Indexing
Numbers, booleans and strings, plus indexing and slicing.
The problem
Every value in a program has a type: a number, some text, a yes/no answer. The type decides what you can do with the value. You can add two numbers, but you can't sensibly add a number to a sentence. Understanding types (and especially strings) removes a huge class of beginner errors.
The concept
Python's core types#
| Type | Example | Used for |
|---|---|---|
int | 42, -7 | Whole numbers: counts, indexes, ages |
float | 3.14, 0.5 | Decimal numbers: prices, scores, probabilities |
bool | True, False | Yes/no answers and conditions |
str | "hello" | Text of any kind |
None | None | "No value yet" or "nothing returned" |
Use type() to check any value:
type(42) # <class 'int'>
type(3.5) # <class 'float'>
type("AI") # <class 'str'>
type(True) # <class 'bool'>
Converting between types#
Values coming from users, files or APIs often arrive as text, even when they look like numbers. Convert them explicitly:
age_text = "25"
age = int(age_text) # 25 (now a number)
price = float("99.50") # 99.5
label = str(10) # '10'
Strings are sequences#
A string is an ordered sequence of characters, so you can reach into it by position (index). Indexes start at 0, and negative indexes count from the end.
s = "Python"
# P y t h o n
# 0 1 2 3 4 5
# -6 -5 -4 -3 -2 -1
s[0] # 'P'
s[-1] # 'n'
Slicing: taking a piece#
A slice uses start:stop:step and returns a new string. The stop index is not included.
s = "Machine Learning"
s[0:7] # 'Machine' (indexes 0–6)
s[8:] # 'Learning' (from 8 to the end)
s[:3] # 'Mac'
s[::2] # 'McieLann' (every 2nd character)
s[::-1] # 'gninraeL enihcaM' (reversed)
Strings are immutable#
You can't change a string in place. s[0] = "J" raises an error. Instead, string operations return new strings:
name = "ada"
name.upper() # 'ADA'
name.capitalize() # 'Ada'
name.replace("a", "o") # 'odo'
print(name) # still 'ada'
Formatting with f-strings#
The cleanest way to build text from values is an f-string:
model, score = "GPT", 0.912
print(f"{model} scored {score:.1%}") # GPT scored 91.2%
Try it
Your task · about 10 minutes
With s = "Machine Learning", print s[0], s[-1], s[0:7] and s[::-1]. Predict each output before you run it.
Recap & next
Key takeaways
- Core types:
int,float,bool,strandNone. Usetype(x)to check a value's type. - Strings are immutable sequences of characters: you can read them but not change them in place.
- Indexing starts at 0; negative indexes count from the end (
s[-1]is the last character). - Slicing is
s[start:stop:step];stopis excluded, ands[::-1]reverses a string. - Convert between types explicitly:
int("42"),float("3.5"),str(10).
Quick check
2 questions · instant feedback
Go deeper
Finished this lesson?
Mark it complete to track your progress and keep your streak going.



