Strings
Class 11 · Computer Science
4 ideas in Class 11 Computer Science, explained in plain words, with games and quizzes to practise.
Creating Strings
Part of: String Basics
Strings group characters in quotes; indexed from zero; immutable sequences; length counts characters.
Why it matters. Text stored. Positions known.
Formulas
- len(s) = last index + 1
- s[i] is valid for i from 0 to len(s) - 1
Key words
- String
- : A sequence of characters in quotes, indexed from zero and unchangeable.
- Index
- : The position number of a character, starting at zero for the first.
- Immutable
- : Locked against change, so no character of a string can be replaced in place.
- Length
- : The count of characters in a string, given by len.
Try it in two minutes
- Quote the text.
- Index from zero.
- Count length.
Try at home. Index your name.
Slicing and Ops
Part of: String Basics
Slices take ranges; plus joins; times repeats; in checks membership; steps skip.
Why it matters. Parts taken. Texts joined.
Formulas
- s[a:b] takes characters from a up to b - 1
- s * n repeats s n times
- x in s is True when x occurs inside s
Key words
- Slice
- : A range s[a:b] taking characters from a up to b - 1.
- Concatenation
- : Joining strings with plus into one longer string.
- Repetition
- : Repeating a string n times with the times operator.
- Membership
- : Testing with in whether a piece occurs inside a string.
- Step
- : The skip size in a slice that picks every nth character.
Try it in two minutes
- Range the slice.
- Join with plus.
- Check with in.
Try at home. Slice a sentence.
String Methods
Part of: Methods and Loops
Upper, lower change case; split breaks; join merges; count, find locate; strip trims spaces.
Why it matters. Text cleaned. Parts found.
Key words
- upper and lower
- : Methods returning the string in full capitals or full small letters.
- split
- : The method breaking a string into a list at each separator.
- join
- : The method merging a list of pieces into one string.
- find
- : The method locating the first position of a piece, or -1 if absent.
- strip
- : The method trimming spaces from both ends of a string.
Try it in two minutes
- Change case?
- Split or join?
- Find or count?
Try at home. Clean a sentence.
Traversing Strings
Part of: Methods and Loops
Loops visit each character; count vowels, check palindromes; strings compare by codes.
Why it matters. Each checked. Patterns found.
Formulas
- s[-1] is the last character of s
- Traverse with: for ch in s
Key words
- Traversal
- : Visiting each character of a string one by one in a loop.
- Palindrome check
- : Testing whether a string reads the same forward and backward.
- Vowel count
- : A loop tally of vowel characters found while traversing.
- Code comparison
- : Ordering strings by the numeric codes of their characters.
Try it in two minutes
- Loop characters.
- Test each.
- Collect result.
Try at home. Count vowels.
What your child will practise
- Creating Strings: Ocean Puzzle (Clue Puzzle)
- Creating Strings: Ocean Quick Fire (Quick Fire)
- Slicing and Ops: Jungle Puzzle (Clue Puzzle)
- String Methods: Ocean Quest (Flip and Match)
- Traversing Strings: Desert Quick Fire (Quick Fire)
- Quiz: Creating Strings (8 questions)
- Quiz: Slicing and Ops (8 questions)
- Quiz: String Methods (8 questions)
- Quiz: Traversing Strings (8 questions)