String manipulation
String positions and slices
A string stores text. Use + to concatenate strings; .length gives the number of characters. ERL and Python both start string positions at zero. ERL .substring(start, count) begins at start and takes count characters; Python's slice stops before its end, so use start + count. .left(n) and .right(n) take characters from either end. ERL .upper and .lower change case; Python calls .upper() and .lower().
word = "Revision"
OUTPUT word.length
OUTPUT word.substring(2, 3)
OUTPUT word.left(4)
OUTPUT word.right(4)
OUTPUT word.upper
OUTPUT word.lower
The outputs include 8, vis, Revi and sion.
Common mistake
Python indexes also start at zero. A slice's end is excluded: ERL substring(2, 3) matches Python [2:5], not [2:3].
Key terms
Character codes
ASC converts a character to its character-set code; CHR converts a code back to a character. Python uses ord() and chr() for the same jobs. For example, the code for B is 66, and 67 represents the next character, C:
code = ASC("B")
OUTPUT code
OUTPUT CHR(code + 1)
Both versions output 66 and then C.
Key terms
Building a username
String operations are often combined. Here, a username is the first three letters of the surname, the first letter of the first name and the year, all in lower case:
first = "Ada"
surname = "Lovelace"
year = 2026
start = surname.left(3)
initial = first.left(1)
username = start.lower + initial.lower + str(year)
print(username)
Both print lova2026. str(year) casts the year to a string, because + can't join a string and an integer.
Joining or adding?
The + operator does different things depending on the data type:
- with two strings, it concatenates them:
"15" + "1"gives"151" - with two numbers, it adds them:
15 + 1gives16
Input from the keyboard always arrives as a string. A program that reads two numbers and joins them with + will print 151 instead of 16, unless the inputs are cast with int() first.
Exam tip
To find the output of a string operation, write the string out with each character's position underneath, starting at 0, then count.