Skip to content
CodeNest

FoundationsLesson 4 of 1916 min

Working with Text

Build, format, slice, and clean up strings — the type you will handle more than any other.

By the end you will be able to

  • Combine strings with concatenation and f-strings
  • Index and slice to extract parts of a string
  • Use common string methods like upper(), strip(), replace(), and split()

A string is text. Single and double quotes work identically — pick whichever avoids escaping the quotes inside your text. Triple quotes span multiple lines.

Python
Output
Hello Hello
It's fine
Line one
Line two
— expected output; press Run to execute it yourself

f-strings: the way to build text

Putting an f before the opening quote lets you drop expressions straight into the text inside {}. This is clearer and less error-prone than gluing pieces together with +.

Python
Output
Hi Ada, you have 20 lessons.
Hi Ada, you have 20 lessons.
Half done means 10.0 lessons.
ADA has 3 letters.
— expected output; press Run to execute it yourself

Indexing and slicing

Characters are numbered from 0. Negative numbers count back from the end, so -1 is the last character.

A slice text[start:stop] takes everything from start up to but not including stop.

Python
Output
P
n
Py
thon
Py
nohtyP
6
— expected output; press Run to execute it yourself

Methods you will use constantly

A method is a function attached to a value, called with a dot: value.method().

Python
Output
CodeNest
CODENEST
Hello
['a', 'b', 'c']
2026-08-09
PYthon
True
True
— expected output; press Run to execute it yourself

Exercise

Given raw = " ada LOVELACE ", produce clean equal to "Ada Lovelace" — trimmed, with each word capitalised. Then print a greeting: Hello, Ada Lovelace!

Python
Output

Press Run to execute this code.

Exercise

Write a function initials(full_name) that returns the uppercase initials of a name. initials("ada lovelace") should return "AL".

Python
Output

Press Run to execute this code.

Check your understanding

0/3 answered
  1. 1.What does "Python"[1:4] produce?
  2. 2.Which produces Total: 7.50 when amount = 7.5?
  3. 3.After s = "hi" and s.upper(), what does print(s) show?