Wednesday, February 9, 2011

Strings can be subscripted

Dad said:
"This is from the Python manual for beginners...(!) I think it's assuming at this point that I know what something would do in C or in Icon.
Strings can be subscripted (indexed);"
Simply put, this means that you can pull out the individual letters (or characters) of a string by using a number indicating their position. Starting with 0 being the first character.

e.g. HELLO

0 = H
1 = E
2 = L
3 = L
4 = O

in it's simplest form, this looks like:

hello_string = "HELLO"
second_character = hello_string[1]

After these two lines have run, the variable second_character will now contain "E"

"...like in C, the first character of a string has subscript (index) 0. There is no separate character type; a character is simply a string of size one."

Some programming languages have a different datatype of variable for characters vs. strings. This is just saying that in Python, a character uses the same datatype as string. By datatype, I mean things like String, Number, Date, Boolean (true/false) etc..

"Like in Icon, substrings can be specified with the
slice notation: two indices separated by a colon."

>>> word[4]
'A'
>>> word[0:2]"

Assuming word == "HelpA":

then word[4] you should understand, because it is similar to the example I gave above.

word[0:2] means the range of characters from index 0 through to, but not including, character 2, as taken from the string.

You will also see examples where these indexes are referenced as negative numbers. e.g. word[0:-1]. Negative indices are counted from the end of the string, instead of the start.

So, continuing with the example variable word from above:

word[-1] will return the last character, or "A"