Theory

Strings have many useful methods:

Case methods:

text = "Hello World"
print(text.upper()) # HELLO WORLD
print(text.lower()) # hello world

Find and replace:

message = "Hello Python"
print(message.replace("Python", "World")) # Hello World

Split and join:

words = "apple,banana,orange"
parts = words.split(",") # ["apple", "banana", "orange"]

fruits = ["a", "b", "c"]
result = "-".join(fruits) # "a-b-c"

Strip whitespace:

text = " hello "
print(text.strip()) # "hello"

Exercises in this lesson

3

Split String

Given: data = "apple,banana,cherry" Split the string by comma to create a list. Print the second fruit (banana).

Easy Points: 15
4

Join List

Given: words = ["Python", "is", "fun"] Join the words with a space " " to create a sentence. Print the result: "Python is fun"

Medium Points: 20
Solve in the playground

Frequently asked questions

What is the lesson “String Methods” about?

Learn useful string methods for text manipulation.

How many exercises are in this lesson?

This lesson has 4 exercises.

Do I need to install anything to do the exercises?

No. The code is written and run in the browser.

Next lesson: List Comprehensions →