Practice Flashcards
Flip to reveal answersWhere do string positions start, and what does a slice include?
Track your progress — Sign up free to save your progress and get smart review reminders based on spaced repetition.
All 5 Flashcards — Substrings
Sign up free to track progress and get spaced-repetition review schedules.
Question
Where do string positions start, and what does a slice include?
Answer
Positions start at 0, and a slice excludes its end position. So text[2:5] gives the characters at 2, 3 and 4 — three characters, which is simply 5 minus 2.
Question
What do len, find and split each do?
Answer
len gives the number of characters. find gives the position where a piece of text starts, or -1 if it is absent. split breaks a string into a list at each occurrence of a separator.
Question
Do string methods change the original string?
Answer
No. upper, lower and replace all return a new string and leave the original untouched. To keep the change you must assign it back: text = text.upper().
Question
How do you split an email address at the @?
Answer
Find the position of the @, then take everything before it and everything from one position after it: at = email.find("@"); user = email[:at]; domain = email[at + 1:]. The +1 skips the @ itself.
Question
When is split better than using fixed positions?
Answer
Whenever the parts can vary in length. code[0:2] works only while the prefix is exactly two characters, whereas splitting at the separator survives a longer or shorter one.
Read the notes
Full study notes for Substrings
Topic 6.1 hub
Programming fundamentals
More from Topic 6.1
All flashcards in this topic
Computer Science exam skills
Paper structures & tips
Track your progress with spaced repetition
Sign up free — Aimnova tells you exactly which cards to review and when, so you remember everything before your IB exam.
Start Free