String Manipulation (OCR GCSE Computer Science): Revision Notes
String Manipulation
In programming, string manipulation refers to the process of modifying, analysing, and working with text data (strings). Strings are a sequence of characters, and being able to manipulate them is essential for tasks like formatting output, taking user input, and processing text in programmes. Below are the basic techniques for string handling, as well as practical ways to use them in programming.
Basic String Manipulation
Finding the Length of a String
You can determine how many characters are in a string using a length function.
Example:
text = "Hello, World!"
length = len(text)
print(length)
Explanation:
- The len() function calculates the total number of characters in the string text.
- In this case, "Hello, World!" contains 13 characters, including the space and punctuation.
Changing Case (Upper/Lower Case)
Upper Case
Converts all letters in a string to uppercase.
Example:
text = "hello"
print(text.upper())
Explanation:
- The upper() method converts all characters in the string text to uppercase letters.
- So "hello" becomes "HELLO".
Lower Case
Converts all letters in a string to lowercase.
Example:
text = "HELLO"
print(text.lower())
Explanation:
- The lower() method converts all characters in the string text to lowercase letters.
- So "HELLO" becomes "hello".
Extracting a Substring (Slicing)
A substring is a smaller part of a string. You can extract part of a string using slicing.
Example:
text = "Hello, World!"
substring = text[0:5]
print(substring)
Explanation:
- The slice text[0:5] extracts characters starting from index 0 up to, but not including, index 5.
- In this case, it extracts the substring "Hello" from "Hello, World!".
Additional Programming Techniques
Concatenation (Joining Strings)
Concatenation is the process of joining two or more strings together to form a single string.
Example:
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name)
Explanation:
- The + operator joins the first_name and last_name variables, adding a space between them.
- The result is "John Doe", which is then stored in full_name and printed.
Replacing Text in a String
You can replace specific parts of a string with new text using the replace() function.
Example:
text = "I love Python"
new_text = text.replace("Python", "programming")
print(new_text)
Explanation:
- The replace() function replaces all occurrences of the word "Python" with "programming" in the string text.
- The new string "I love programming" is then printed.
Searching Within a String
You can check whether a specific substring is present in a string using the in keyword.
Example:
text = "I love programming"
if "love" in text:
print("The word 'love' is in the string!")
Explanation:
- The in keyword checks if the substring "love" exists within the string text.
- Since it does, the condition evaluates to True, and the programme prints "The word 'love' is in the string!".
Practical Use
Students can use string manipulation techniques in various classroom programming tasks:
- Concatenation: Useful for combining data like first and last names, or building custom messages based on user input.
- Slicing: Students can slice strings to extract important information, such as getting the first three letters of a word or parsing data from a larger text.
- Changing Case: Used to convert user input to a consistent format for easier comparison (e.g., making everything lowercase when checking for matching usernames).
- Replacing Text: Students may use this to create a text-based game where certain words need to be replaced to alter the story dynamically.
- Length Check: Used to ensure inputs like passwords meet length requirements (e.g., a password must be at least 8 characters long).
Key Points to Remember
- Concatenation allows you to join strings together, such as creating full names from first and last names.
- Slicing helps you extract specific parts of a string, like the first three characters or a specific word.
- Changing case (upper or lower) ensures uniformity when comparing or formatting strings.
- Basic string functions like finding length, replacing, and searching are essential for working with and manipulating text effectively in your programmes.