Programming constructs - CCEAString manipulation

Computer programs use data stores to organise the different types of data they contain. Data stores can be either a constant, variable or an array, and contain inputs, outputs, functions, instructions, sequences and more.

Part ofDigital Technology (CCEA)Digital development concepts (programming)

String manipulation

Strings are a collection of stored under one identifier. They can be thought of as an array of characters. There are a variety of different ways we can manipulate strings.

As a string behaves like an array of characters, we can access individual characters in a string. Strings are referenced by , starting at 0.

name = 'walter'
name[0] = w
name[1] = a
name[2] = l
name[3] = t
name[4] = e
name[5] = r

Concatenation

Strings can be joined together to form a new string of data. This is called concatenation. We use the + symbol to concatenate strings. For example:

fname = 'walter'
sname = 'white'
name = fname + ' ' + sname

This will produce the output: walter white. Note the space (' ').

Character searching

We can determine if an individual character is found in a string using the 'in' keyword. This will return True/False:

'f' in sname = False
'e' in fname = True

This also works with substrings (subset of characters):

'alt' in fname = True

String splitting

It is possible to find part of a string by using a subString (a bit, or part, of a string) method. Remember that the first character in the string starts at reference 0.

For example, select a substring from character 2 to character 4 (remember, the slice will stop before element 4):

fname.subString(2,4) = lt

In Python, this can be implemented as:

fname[2:4] = lt

Python provides the ability to slice strings. Slicing characters from a string allows programmers to select any characters that they want from a string.

You can set the element you want to start the slice at, which element you want to finish at, and the increments or decrements you want. For example:

fname[0:5:2] = wle

This returns a character, then moves forward 2 positions and returns the next character until character 5.

Common string manipulation techniques

CommandDescriptionExampleOutput
.lengthThis returns the number of characters in the string. This will include spacesfname.length6
.upperThis changes all the characters in the string to upper casename.upperWALTER
.lowerThis changes all the characters in the string to lower casename.lowerwalter
[n]This selects a particular character in the stringfname[2]l
.subString(x,y)This selects a particular part of a string. It starts at the place in the string set by x and selects the next character, set by yfname.subString(1,2)al
str()This converts any data into the string data typestr(fname)-
ASCThis will return the ascii number of the characterASC(fname[0])A - 119
ChrThis will return the ascii character of a numberCHR(65)A
Command.length
DescriptionThis returns the number of characters in the string. This will include spaces
Examplefname.length
Output6
Command.upper
DescriptionThis changes all the characters in the string to upper case
Examplename.upper
OutputWALTER
Command.lower
DescriptionThis changes all the characters in the string to lower case
Examplename.lower
Outputwalter
Command[n]
DescriptionThis selects a particular character in the string
Examplefname[2]
Outputl
Command.subString(x,y)
DescriptionThis selects a particular part of a string. It starts at the place in the string set by x and selects the next character, set by y
Examplefname.subString(1,2)
Outputal
Commandstr()
DescriptionThis converts any data into the string data type
Examplestr(fname)
Output-
CommandASC
DescriptionThis will return the ascii number of the character
ExampleASC(fname[0])
OutputA - 119
CommandChr
DescriptionThis will return the ascii character of a number
ExampleCHR(65)
OutputA