Computer Science
Grade 7
20 min
String Operations
String Operations
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Extract a portion of a string using slicing notation.
Use common string methods like `.upper()`, `.lower()`, `.find()`, and `.replace()` to manipulate strings.
Iterate through a string character by character using a `for` loop.
Check if a substring exists within a larger string using the `in` keyword.
Explain the concept of string immutability.
Combine string operations to solve multi-step problems.
Ever wonder how a game knows to display 'PLAYER 1 WINS!' in all caps, or how a search bar instantly finds a word in a long article? 🕵️ Let's learn the coding magic behind it!
In this lesson, you'll move beyond just joining strings together. We will explore powerful tools to slice, search, change, and analyze strings, which are essential sk...
2
Key Concepts & Vocabulary
TermDefinitionExample
String SlicingThe process of creating a new substring by extracting a sequence of characters from an existing string.If `word = 'Computer'`, then `word[0:4]` is a slice that results in the new string `'Comp'`.
String MethodA special function that is built-in to all strings and is used to perform an operation on that string.The `.upper()` method on the string `'hello'` returns a new string, `'HELLO'`.
IterationThe process of repeating a set of instructions for each item in a sequence. For strings, this means doing something for each character, one by one.Using a `for` loop to print each character of `'Code'` on a new line.
SubstringA smaller string that exists inside of a larger string.In the string `'Programming&...
3
Core Syntax & Patterns
Slicing Syntax
my_string[start_index:end_index]
Used to get a piece of a string. The slice includes the character at `start_index` but goes up to, and does not include, the character at `end_index`.
Method Call Syntax
my_string.method_name(arguments)
Used to run a string method. The string comes first, followed by a dot, the method name, and parentheses. Some methods need extra information (arguments) inside the parentheses.
String Iteration Pattern
for character in my_string:
# code to run for each character
A `for` loop used to process each character of a string one at a time. In each loop, the `character` variable holds the current character being processed.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
Which code snippet correctly reverses the string `s = 'Hello'` into `'olleH'`?
A.`for char in s: s = char + s`
B.`s.reverse()`
C.`reversed_s = ''; for char in s: reversed_s = reversed_s + char`
D.`reversed_s = ''; for char in s: reversed_s = char + reversed_s`
Challenging
Given `email = 'ada.lovelace@example.com'`, how can you extract just the domain name 'example.com'?
A.Find the index of '.' and slice from there.
B.Find the index of '@', add 1, and slice from that position to the end of the string.
C.Replace the username with an empty string.
D.Use `email.find('.com')` and slice.
Challenging
A program needs to create a new string containing only the consonants from `phrase = 'Hello World'`. Which logic is correct?
A.Iterate through `phrase.lower()`. If a character is NOT in 'aeiou ' (vowels and space), add it to a new string.
B.Iterate through `phrase`. If a character is in 'bcdfghjklmnpqrstvwxyz', add it to a new string.
C.Use `phrase.replace()` for each vowel and the space.
D.Slice out the vowels from the original string.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free