BrightPath
Back to Course
Year 10 Coding

Python String Methods

Master essential string operations including upper, lower, split, join, and slicing to manipulate text data in Python.

Changing Case: upper(), lower(), title()

Strings in Python are immutable -- they cannot be changed in place. Instead, string methods return a new string. Python provides several methods to change the case of text:

name = "alice smith"

print(name.upper())     # Output: ALICE SMITH
print(name.lower())     # Output: alice smith
print(name.title())     # Output: Alice Smith
print(name.capitalize())# Output: Alice smith

# The original string is NOT changed
print(name)             # Output: alice smith

These methods are useful for formatting user input, comparing strings case-insensitively, or displaying text consistently. For example, user_input.lower() == "yes" handles "YES", "Yes", and "yes" equally.

Splitting and Joining: split() and join()

The split() method breaks a string into a list of substrings based on a separator. The join() method does the reverse -- it combines a list of strings into one string with a separator between them.

# split() -- break a string into a list
sentence = "Python is awesome"
words = sentence.split()        # Split on spaces (default)
print(words)                    # Output: ['Python', 'is', 'awesome']

csv_data = "Mia,10,Sydney"
fields = csv_data.split(",")    # Split on commas
print(fields)                   # Output: ['Mia', '10', 'Sydney']

# join() -- combine a list into a string
fruits = ["apple", "banana", "cherry"]
result = ", ".join(fruits)
print(result)                   # Output: apple, banana, cherry

path = "/".join(["home", "student", "documents"])
print(path)                     # Output: home/student/documents

split() and join() are commonly used together to clean, transform, and reassemble text data.

String Slicing

Slicing lets you extract part of a string using the syntax string[start:stop:step]. The start index is included, but stop is excluded. Python uses zero-based indexing -- the first character is at index 0.

text = "BrightPath"
#       B r i g h t P a t h
#       0 1 2 3 4 5 6 7 8 9

print(text[0])        # Output: B       (first character)
print(text[6:])       # Output: Path    (index 6 to end)
print(text[:6])       # Output: Bright  (start to index 5)
print(text[2:7])      # Output: ightp   (index 2 to 6)
print(text[-4:])      # Output: Path    (last 4 characters)
print(text[::-1])     # Output: htaPthgirB  (reversed)

Negative indices count from the end: -1 is the last character, -2 is the second-last, and so on. A step of -1 reverses the string.

Key Vocabulary

String Method

A built-in function called on a string using dot notation (e.g. "hello".upper()) that returns a new string.

Immutable

An object that cannot be changed after creation. Strings are immutable -- methods return new strings rather than modifying the original.

Slicing

Extracting a portion of a string using index notation: string[start:stop:step].

Zero-Based Indexing

A system where the first element is at index 0, the second at 1, and so on.

Worked Examples

1

Clean up a user's name input so it has proper title case and no extra spaces.

raw_input = "  jOHN   sMITH  "
clean_name = raw_input.strip().title()
print(clean_name)     # Output: John Smith

Step 1: Use .strip() to remove leading and trailing whitespace.

Step 2: Chain .title() to capitalise the first letter of each word.

Result: The messy input becomes a properly formatted name.

2

Split a CSV line and access individual fields.

record = "Ava,15,Year 10,Sydney"
parts = record.split(",")

name = parts[0]        # "Ava"
age = parts[1]         # "15"
year_level = parts[2]  # "Year 10"

print(f"{name} is {age} years old and in {year_level}.")
# Output: Ava is 15 years old and in Year 10.

Step 1: Use .split(",") to break the string at every comma.

Step 2: Access each field by its index in the resulting list.

Step 3: Use an f-string to format the output.

3

Extract the file extension from a filename using slicing.

filename = "report_2026.pdf"

# Method 1: Using split
extension = filename.split(".")[-1]
print(extension)      # Output: pdf

# Method 2: Using slicing with rfind
dot_pos = filename.rfind(".")
extension = filename[dot_pos + 1:]
print(extension)      # Output: pdf

Method 1: Split on "." and take the last element with [-1].

Method 2: Find the position of the last dot with .rfind("."), then slice from one character after it.

Both approaches give the same result: pdf.

Knowledge Check

Select the correct answer for each question. Click "Check Answer" to see if you are right.

Question 1

What does "hello world".upper() return?

Question 2

What does "apple,banana,cherry".split(",") return?

Question 3

Given text = "Python", what does text[1:4] return?

Question 4

What does " - ".join(["2026", "03", "01"]) return?

Question 5

Why are Python strings called "immutable"?

Key Concepts Summary

Year 10: Python Functions Year 10: Dictionaries