a string is a list of characters. to access a specific letter in a sting, you would refer to the index of the character like its a list
Note: List start with 0 (ie first character is index 0, second is index 1, third is index 2, etc). Index values must also be integers - you get a type error otherwise
a list method will work on strings and by treating each character as a list item; this includes spaces. if you want to ge the length of a string, you can is the len() method with the string as the argument
a common programming pattern to is process a string one character at a time, also known as a traversal. one way to do this is through a while loop:
# prints string backwards one letter at a time
string = "fruit"
index = 0
while index < len(string):
index += 1
print(string[-(index)]) # negative index start at the end of string and work forwards
this can be down with for loops too
fruit = "peach"
for char in fruit:
print(char)
Now let’s try to select slices of the string. to do this you select two index’s - with a colon between - that act as the range of the slice. if you leave out one of the numbers, the range will go to the end of that side of the string
string = 'Monty Python'
print(string[0:5]) # prints: Monty
print(string[6:12]) # prints: Python
Note: Strings are immutable and can not be modified once declared - you can only create new offshoots of a string. It may be tempting to try to redefine letters of a string but it will give you an error
If you use comparison operators with strings, they treat each character as there unicode value and compare those.
Some common methods involving strings include
you can find more methods with the dir method which provides ever native method for a give value type
*Note: you can format files into strings with the percent operator (this is really awkward and not recommend)