大象传媒

Data types, structures and operators - EdexcelString manipulation

Programs use data, known as 鈥榲alues鈥. Variables hold values. Each variable in a program must have a data type. Sometimes a programmer needs to store a lot of related data. To do this they use structures such as arrays.

Part of Computer ScienceApplication of computational thinking

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 examples below use to demonstrate string manipulation:

wordOne = "Computer"

wordTwo = "Science"

Length

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

len(wordOne) would give the answer 8 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 鈥淐omputer鈥 - remember computers generally start counting at zero.

wordOne[0:2] would give "Com", the first three characters in the string.

wordOne[3:6] 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 upper- or lowercase. This can be very useful, for example when checking possible inputs.

topic = "Computer Science".topic = topic.lower() would give a value for topic of "computer science"

topic = topic.upper() would give a value for topic of "COMPUTER SCIENCE".

Concatenation

To strings means to join them to form another string, eg:

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".