Module 4 · Python for Beginners
Working with Text
Strings in depth: quotes and special characters, joining and repeating, length, indexing and slicing, the most useful string methods, splitting and joining, and f-string formatting.
About 30 minutes
Open in ColabEvery example as a notebook you can run, edit and save to your Google Drive.
Strings
Text in Python is a string (str): a sequence of characters in quotes. Single and double quotes work the same way:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
Economics ECO 201Use double quotes when the text contains an apostrophe, or single quotes when it contains double quotes:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
I'm in 200 level
She said "well done"Special characters
A backslash \ starts a special character inside a string:
| Write | Means |
|---|---|
\n | a new line |
\t | a tab |
\' or \" | a quote inside quotes of the same kind |
\\ | one backslash |
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
Name: Adaeze
Level: 300For text over several lines, use triple quotes:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
Block C, Room 14
Queen Amina Hall
University of LagosJoining and repeating
+ joins strings; * repeats one:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
Chidi Okeke
--------------------Length
len() counts the characters, including spaces:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
11Indexing: one character
Each character has a position, its index, starting at 0:
| Character | P | y | t | h | o | n |
|---|---|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 | 4 | 5 |
| From the end | -6 | -5 | -4 | -3 | -2 | -1 |
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
P
t
nNegative indexes count from the end, so [-1] is always the last character, however long the string.
Slicing: part of a string
text[start:stop] takes the characters from start up to, but not including, stop:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
ECO
201
2026
ECO201
2026- Leave out
startto begin at the start; leave outstopto go to the end. - The number of characters you get is
stop − start:[0:3]gives 3.
String methods
A method is a function that belongs to a value. You call it with a dot: text.upper(). String methods never change the original string (strings can't be changed); they give you a new one.
Changing case
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
ADAEZE OKAFOR
adaeze okafor
Adaeze Okafor
Adaeze okaforRemoving spaces
Data typed by people often has stray spaces. strip() removes them from both ends:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
[ lagos ]
[lagos]
[Lagos]You can chain methods, as in the last line: strip, then title-case. lstrip() and rstrip() remove spaces from one side only.
Finding and replacing
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
musa.bello@unilag.edu.ng
10
2
True
True
Truefindgives the index where something first appears, or-1if it isn't there.inasks "is this inside?" and givesTrueorFalse.
Checking what's in a string
| Method | True when the string... | "2026" | "ECO" | "ECO 201" |
|---|---|---|---|---|
isdigit() | is all digits | True | False | False |
isalpha() | is all letters | False | True | False |
isupper() | has no lowercase letters | False | True | True |
These are useful for checking input before converting it, for example if text.isdigit(): number = int(text).
Splitting and joining
split() breaks a string into a list of pieces. With no argument, it splits on spaces:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
['Data', 'analysis', 'with', 'Python']
['Adaeze', 'Economics', '200', '4.21']The second example is how a line of a CSV file breaks into its values. (Each piece is still text: "200", not 200.)
join() does the opposite: it glues a list of strings together, with the string you call it on between each piece:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
2026-09-15
Lagos, Abuja, Kanof-strings in depth
An f-string puts values inside text. Anything in {} is worked out and inserted:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
Musa owes ₦45,000
Half is 22500.0
In capitals: MUSAAfter the value, a colon and a format control how it looks:
| Format | Does | Example | Shows |
|---|---|---|---|
:, | thousands separators | {45000:,} | 45,000 |
:.2f | 2 decimal places | {3.14159:.2f} | 3.14 |
:.0% | as a percentage | {0.25:.0%} | 25% |
:<10 | left-align in 10 spaces | {"Food":<10} | Food |
:>10 | right-align in 10 spaces | {4500:>10} | 4500 |
:^10 | centre in 10 spaces | {"Hi":^10} | Hi |
Alignment lines up simple tables:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
Item Cost
Food 28,000
Data 6,000When it goes wrong
word = "Python"
word[0] = "J"Output
TypeError: 'str' object does not support item assignmentStrings can't be changed in place. Make a new one instead: word = "J" + word[1:].
print("Python"[10])Output
IndexError: string index out of range"Python" has positions 0 to 5. Slices don't raise this error ("Python"[3:10] gives "hon"), but single indexes do.
name = "musa"
name.upper()
print(name)Output
musaNo error, but no change either: upper() returned a new string, and it was thrown away. Store it: name = name.upper().
Summary
| You want to... | Write |
|---|---|
| join text | first + " " + last |
| count characters | len(text) |
| one character | text[0], text[-1] |
| part of the text | text[start:stop] |
| change case | .upper(), .lower(), .title() |
| remove stray spaces | .strip() |
| replace | .replace("old", "new") |
| check if it contains | "x" in text |
| split into a list | text.split(",") |
| join a list | ", ".join(items) |
| format a value | f"{value:,.2f}" |
Try it
Practice
code = "ECO201-2026". What does code[3:6] give? Type it without quotes.
Practice
What does " abuja ".strip().title() give? Type it without quotes.
Practice
line = "Adaeze,Economics,200,4.21". How many items are in line.split(",")?
Task
10 minClean up a messy name and email: start with name = " aDAEZE okafor ". Print the name stripped and in title case, print its initials (first letter of each part, using split() and indexing), and build an email like adaeze.okafor@student.edu.ng from it in lowercase. Paste your code.
Your work is checked for
- Uses strip()
- Uses title()
- Splits the name
- Indexes a character with [0]
- Makes it lowercase
- Builds an email with @
Earn your badge
Take the module check
Questions on this module. Pass and you earn the Python Text badge, free.
Create a free account to earn the badge