B2.1.2

Construct programs that can extract or manipulate strings

Find the index when the substring first appears print(message.find("e")) 1 print(message.find("Hel")) 0

1 min read65 words
message = "Hello World"

print(message[4])  # o

# Find the index when the substring first appears
print(message.find("e"))    # 1
print(message.find("Hel"))  # 0

# Find the last instance of the substring in the string
print(message.rfind("o"))   # 7

# Replace one substring with another
print(message.replace("World", "IBDP"))  # reminder: there's a third argument for this function

# Split the string into a list
messageList = message.split()
# Output: Hello World
for i in messageList:
    print(i, end=" ")

Start typing to search all published objectives.