String slices
Slices of string includes the first value and excludes the last value.
Negative indexing works as well, but you still cannot “wrap around” the slice more than once.
Striding allows you to skip values in an array, and negative striding lets you start at the end of the string and work backwards.
Strings are immutable in Python, so you need to use slices of replace()
to change them.
String Methods
find()
returns -1
if the sub string is not found. index()
will throw a ValueError
exception.
center()
allows you to easily center text with characters.
expandtabs()
lets you easily replace \t
characters with spaces.
ljust()
& rjust()
let you left and right justify a piece of text.
lstrip()
, rstrip()
, and string()
let you remove whitespace on the left, right, and both sides of a string. They
also take a list of characters that it will remove.
zfill()
allows you to pad strings and integers with 0s, and it respects signs.
Evolution of Character Codes
The Evolution of Character Codes Seems to be a decent paper on how we got to where we are today.
Encoding and Counting
A hexadecimal number is represented by 16 bits, or as I like to think of “slots” of on-or-off. While it is true that F
is the 16th value we can use in hexadecimal, it represents 15
because 0
is the 1st value, but represents 0
.
This also means 0xF
in hex is 0b1111
in binary - and they all equal 15
in a decimal format.
For whatever reason my brain decided it wanted to lose an hour of its life figuring this out, insisting that F
should
equal 16, because obviously, 0 equals 1. Madness.
Similarly, in octal
, 7
is the 8th value we can use, and it represents 7, because 0
is the 1st value, but represents 0
.
Negative Striding
So negative striding was also tripping me up because you have to remember you’re going to always start at the end of the sequence, include the “first” element, and then backwards.
|
|