Programming technique · B2.1.2
String manipulation
A string is a sequence of characters. Java provides methods for measuring, locating, extracting and creating changed text while the original String value remains immutable.
A string is a sequence of characters. Python provides functions, methods and slicing for measuring, locating, extracting and creating changed text while the original string value remains immutable.
Useful String methods
Useful string operations
substring(0, 4) includes indexes 0–3, so the result is Mira.
city[0:4] includes indexes 0–3, so the result is Mira.
| Method | Purpose | Important detail |
|---|---|---|
length() | Number of characters | The first character is still at index 0. |
charAt(index) | One character | Returns a char. |
substring(start, end) | Part of a string | Includes start but excludes end. |
indexOf(text) | First position of text | Returns -1 when not found. |
replace(old, new) | Replacement copy | Returns a new String; the original value is unchanged. |
toUpperCase() | Uppercase copy | Strings are immutable; store or use the returned value. |
| Operation | Purpose | Important detail |
|---|---|---|
len(text) | Number of characters | The first character is still at index 0. |
text[index] | One-character string | A position outside the string raises IndexError. |
text[start:end] | Part of a string | Includes start but excludes end. |
text.find(value) | First position of text | Returns -1 when not found. |
text.replace(old, new) | Replacement copy | Returns a new string; the original value is unchanged. |
text.upper() | Uppercase copy | Strings are immutable; store or use the returned value. |
Find a separator, then extract
Alter text by building a new string
Because strings are immutable in both languages, “changing a string” normally means creating a new string value from extracted pieces, concatenation or a replacement operation.
Check your understanding
Answer each question before opening the model answer.
-
For the text "Miraflores", what does the range 0 to 4 return when the end index is excluded?
Reveal model answer
Mira, because indexes 0, 1, 2 and 3 are included and index 4 is excluded.
-
What does indexOf/find returning -1 mean?
Reveal model answer
The requested substring was not found. Check that result before using it as an extraction position.
-
Does replace() modify the existing string object in place?
Reveal model answer
No. In both examples it returns a new string value; store or use that returned value.
Zero-based indexing
The first character is index 0, not index 1.
End index excluded
substring(0, 4) returns characters at indexes 0, 1, 2 and 3.
End index excluded
text[0:4] returns characters at indexes 0, 1, 2 and 3.
Check before extracting
A missing separator or an invalid character position can make the result wrong or cause a runtime error.
Challenges Choose one
Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.
Initial and Surname
SelectedAsk the user for a forename and surname. Output the first initial followed by the surname in uppercase. For example, ben white should become B WHITE.
Airline Ticket Code
SelectedAsk the user for two city names. Output the first four characters of each city in uppercase, separated by a dash. For example, London and Madrid should produce LOND-MADR. State what your program expects if a city name contains fewer than four characters.
Name Separator
SelectedAsk the user to enter a forename and surname on one line. Find the space and extract the two names into separate variables. Output each part with a clear label.
Word Extractor
SelectedStore the sentence "Quick brown fox jumps over the lazy dog." Ask the user which word should be removed. Find the word, rebuild the sentence using substring extraction and concatenation, and output the revised sentence. Your program should respond clearly when the word is not present.
Selected challenge
This choice is shared with the portfolio setup page.
Plan your solution in handwritten pseudocode
Before opening your IDE or writing any program code, handwrite pseudocode for this challenge on paper.
Not marked complete. If you submit now, the GitHub README will record “No”.
Create your challenge folder
Run this command after planning. It creates the correct empty folder inside your portfolio.
Complete the challenge
Use your handwritten pseudocode as the starting plan, then write and test your solution in the folder created above.
Optional two-level scaffold
Try from your handwritten pseudocode first. Scaffold gives some structure; Scaffold + comments gives stronger guidance. Use only the level you need, and update your pseudocode first if the support changes your plan.
Submit for review
Run this when your program is complete. It creates the README, commits the folder and pushes it. The README records whether you marked the handwritten pseudocode as complete; the paper itself is handed to your teacher separately.