Built-in String Functions
Strings have methods that you can call on them. However, there are also built-in functions that are relevant to strings. A few that we'll use include:
- len(s) - the length of a string
- ord(s) - the code point of the character supplied
- chr(i) - the character represented by the supplied code point
The Built-In len() Function
len is a built-in function that returns the length of a sequence
- it is not a method, so you do not call it on an object
- however, you can pass a value to it, and it will return its length
- for strings, it will return the number of characters
Index of the Last Character in a String
What's the formula for getting the last character in a string? →
Index of the Last Character in a String Continued
What values / expressions would you use if you wanted to index into the string below to get the last character? →
Representing Strings
There's a standard for consistently representing text through different character sets and encodings.
- this system is called unicode
- in unicode, code points are numbers (specifically integers) that represents a character or glyph
- here's a table of code points
- here's a unicode snowman
Unicode Table
Let's see that table again.
- note that 65-90 are uppercase latin letters; 97-122 are lowercase
- this matches an older method of encoding characters called ASCII
- ASCII accomodated 128 characters
- what are some shortcomings of this? →
chr() and ord()
Python has a couple of functions that translate code points to characters and characters back to code points.
- ord(c) - gives back the integer value of the code point of the supplied character
- chr(i) - gives back the character corresponding to the supplied integer (code point)