Programming techniques - OCRThe use of basic string manipulation

Proficient programming requires knowledge of many techniques. These techniques allow for powerful, complex programs.

Part ofComputer ScienceComputational thinking, algorithms and programming

The use of basic string manipulation

A is a that holds a sequence of one or more alphanumeric characters. It is usually possible to manipulate a string to provide information or to alter the contents of a string.

The following examples use strings:

wordOne = "Computer"
wordTwo = "Science"

Length

The length of a string can usually be determined using the len. This gives the length as an .

len(wordOne) - would give the answer eight, as there are eight characters in the word "Computer"

Character position

It is possible to determine which character features at a position within a string:

wordOne[2] - would give the answer "m", as "m" is the third character in the word “Computer” (remember computers start counting at zero)

wordOne[0,3] - would give "Com", the first three characters in the string

wordOne[3,3] - would give "put", the three characters starting from position three

Upper and lowercase

It is possible to change all letters in a string to either lowercase or uppercase. This can be very useful, for example when checking possible inputs:

topic = "Computer Science"

topic = topic.lower - would give a value for the topic of "computer science"

topic = topic.upper - would give a value for the topic of "COMPUTER SCIENCE"

Concatenation

To strings means to join them to form another string. For example:

sentence = wordOne + " " + wordTwo - would give "Computer Science"

Alternatively, a string can be lengthened by adding more characters. For example:

wordOne = wordOne + " Science" - would result in the value of wordOne becoming "Computer Science"