Back to Topic 6.1 — Programming fundamentals
6.1.2Computer Science SL5 flashcards

Substrings

Practice Flashcards

Flip to reveal answers
Card 1 of 56.1.2
6.1.2
Question

Where do string positions start, and what does a slice include?

Click to reveal answer

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.

Card 1concept

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.

Card 2definition

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.

Card 3concept

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().

Card 4process

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.

Card 5concept

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.

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
IB Computer Science Substrings Flashcards | 6.1.2 | Aimnova