Lesson 4: Strings & String Methods

1. Working with Strings

Strings are text — enclosed in " " or ' '

Basic operations:

name = "Alex"
greeting = "Hello, " + name  # concatenation
repeat = "Hi" * 3            # HiHiHi

Access characters: name[0] = 'A', name[-1] = 'x'

Exercise 1

Let

s = "Machine Learning"
s[0] = 
s[-1] = 
len(s) = 

2. Useful String Methods

Example:

text = " hello world "
clean = text.strip().upper()  # "HELLO WORLD"

Exercise 2

What does "data science".replace("data", "machine") produce?

Exercise 3

Which methods return a new string?
← Previous Lesson (3) Next Lesson (5) →