Revise Computingrevisecomputing.co.uk
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates

Knowledge organisers / String Manipulation

Substring slicing

All topicsPractise exam questions
Knowledge organiser

String Manipulation

301.11b

Extract part of a string.

What you need to know

Slicing extracts a portion of a string using [start:end]. The start index is included, the end index is excluded. Indices start at 0.

Key points

  • text[0] is the first character
  • text[0:3] gives characters at index 0, 1, 2
  • text[-1] is the last character
  • text[:3] means from start to index 2
  • text[3:] means from index 3 to the end

Code examples

Slicing strings
python
word = "Python"
print(word[0])     # P
print(word[0:3])   # Pyt
print(word[-1])    # n
print(word[2:])    # thon