Programming technique
String manipulation
A string is a sequence of characters. Java provides methods for measuring, locating, extracting and changing text without changing the original string value.
A string is a sequence of characters. Python provides functions, methods and slicing for measuring, locating, extracting and changing text without changing the original string value.
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. |
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.upper() | Uppercase copy | Strings are immutable; store or use the returned value. |
Find a separator, then extract
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, remove it using substring operations 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.
Create your challenge folder
Run this command before starting. It creates the correct empty folder inside your portfolio.
Complete the challenge
Write and test your own solution in the folder created above.
Optional scaffolded support
Use only as much support as you need. The templates organise the program but leave the important algorithm unfinished.
Submit for review
Run this when your program is complete. It creates the README, commits the folder and pushes it.