greet = "Hello Bob"
print(greet[0])
print(greet[0], greet[2], greet[4])
x = 8
print(greet[x - 2])
print(greet[-1])
print(greet[-3])
print(greet[50]) # Error
print(greet[-(len(greet)+1)]) # This throws an error,
# it implies a character before the start of the string
The length of a string is given by the "len()" function
s = "A long string"
print(len(s))
# The empty string case
s = ""
print(len(s))
# A String with whitespace character
s = "\t"
print(len(s))
s = "A long string"
# Realise that a character is just another string in Python
# In some languages, like C/C++, individual characters
# are not strings but have a different type, but Python
# treats them as a single character string
print(type(s[0])) # Prints str
print(len(s[0])) # Prints 1
# Beyond indexing, you can slice strings to create substrings
greet = "Hello Bob"
print(greet[0:3]) # The 'prefix' substring of the first 3 characters
print(greet[3:3]) # The interval [3, 3) is empty
print(greet[5:8])
# Negative length strings?
print(greet[6:0]) # If the second index occurs before the first index it won't
# throw an error, just make a zero length (empty) string
print(greet[:5]) # This is the same as greet[0:5]
# greet[:n] is called a prefix of greet, where n is in [0, len(greet))
print(greet[5:]) # This is the same as greet[5:9]
# greet[n:] is called a suffix of greet, where n is in [0, len(greet))
print(greet[:]) # This is just the whole string, allowing you to make
# a copy of the string
s = "Strings can't be changed"
# This doesn't work
s[0] = 's'
# To make s lower case you could instead do:
s = 's' + s[1:]
print(s)
L = [2, 1, 3]
L[1] = 5
a = 1
b = a
print(a)
print(b)
warm = ['red', 'yellow', 'orange']
hot = warm
hot.append('pink')
print(hot)
print(warm)
cool = ['blue', 'green', 'gray']
chill = cool[:]
chill.append('black')
print(chill)
print(cool)
chill = cool[:]
warm = ['yellow', 'orange']
hot = ['red']
brightcolors = []
brightcolors.append(warm)
brightcolors.append(hot)
print(brightcolors)
# [['yellow', 'orange'], ['red']]
hot.append('pink')
print(hot)
# ['red', 'pink']
print(brightcolors)
# [['yellow', 'orange'], ['red', 'pink']]