Strings

Coding: Introduction to Python v3, topic 2.1.2

Strings

Strings are a special case of an array or list, where each element is a character. There are many methods we can apply to them:

Concatenation

Concatenation

  • Concatenation is joining two strings together.
  • This can be done with the + sign. E.g:
  • "Hello " + "World" = "Hello World".
Accessing a character

Accessing a character

  • Remember, strings are just lists. So we can access a specific character in the exact same way as we access a list element:
  • string[3] would get the fourth character.
Length of a string

Length of a string

  • The length of a string is given by the len function:
  • len("Hello") would give 5.
Substrings

Substrings

  • A substring is a part of a longer string.
  • In Python we take a substring using:
  • stringname[startPos:endPos]
  • This takes a substring starting at the 'startPos'th character, and finishing at the 'endPos'th character.

Test yourself on Strings

  • What is concatenation?

Practise this topic for free