GCSE Computer Science (CCEA)

Topic Summaries

ALGORITHMS AND PROGRAMMING: Programming techniques

String manipulation

  • String: a sequence of characters (letters, numbers, symbols). Strings are written inside quotation marks. In Python, strings are immutable (meaning they cannot be changed directly).

name = “Ada

message = “Hello

  • Concatenation: joining two or more strings together.

= “Ada

Lovelace

first_name + “ ” + last_name # “Ada Lovelace”

  • Indexing: each character has an index number, starting from 0.
  • Slicing: extracting parts of a string using indexes. This is used for extracting initials, substrings, etc.

word = “computer

 
 

print(word[0]) # C

print(word[3]) # p

print(word[–1]) # r

 
 

print(word[0:4]) # Comp

print(word[3:]) # puter

print(word[:8]) # Compute

 
 

print(len(text)) # 8

  • Other common methods of string manipulation are: 
    • upper() and lower() to change to uppercase/lowercase. 
    • strip() to remove spaces 
    • find() to locate a character or substring. This returns the index of the first match, or returns –1 if not found.

Unlock String manipulation

Sign up to unlock the rest of this topic — plus every other topic, video, flashcard set and eBook guide in your SnapRevise Digital subscription.

Cancel anytime — instant access the moment you sign up.