• Strings are not like integers, floats, and booleans.
• A string is a sequence, ----ordered collection of other values.
• In this chapter ---see how to access the characters that make up a string ----
learn about some of the methods strings
• A String Is a Sequence
• A string ---- sequence of characters.
• You can access---- the characters one at a time---with the bracket operator:
• >>> fruit = 'banana'
• >>> letter = fruit[1]
• The second statement----- selects character number 1 from fruit and assigns it to
letter.
• The expression in brackets ----- called an index.
• The index indicates---- which character in the sequence you want (hence the name).
• But you might not get what you expect:
• >>> letter
• 'a'
• For most people---- the first letter of 'banana' is b, not a.
• But for computer scientists-------the index is an offset----- from the beginning of
the string, and the offset of the first letter ----is zero.
• >>> letter = fruit[0]
• >>> letter
• 'b'
• So b---- the 0th letter ----of 'banana', a ---- the 1st letter and n -----2th letter
• As an index, you can use an expression that contains variables and operators:
• >>> i = 1
• >>> fruit[i]
• 'a'
• >>> fruit[i+1]
• 'n'
• But the value of the index has to be an integer. Otherwise you get:
• >>> letter = fruit[1.5]
• TypeError: string indices must be integers
• String Slices
• A segment of a string is called a slice.
• Selecting a slice is similar to selecting a character:
• >>> s = 'Monty Python'
• >>> s[0:5]
• 'Monty'
• >>> s[6:12]
• 'Python'
• The operator [n:m] returns the part of the string----- from the “n-eth”
character to the “m-eth” character
• Including the first but excluding the last.
• This behavior is counter‐intuitive(does not happen---we expect)----- but it
might help to imagine------ the indices pointing between the characters, as in
Figure
• If you omit the first index (before the colon), the slice starts at the beginning
of the string.
• If you omit the second index, the slice goes to the end of the string:
• >>> fruit = 'banana'
• >>> fruit[:3]
• 'ban'
• >>> fruit[3:]
• 'ana'
• If the first index---- is greater than or equal to the second----- the result is an
empty string-----represented by two quotation marks:
• fruit = 'banana'
• >>> fruit[3:3]
• ''
• An empty string contains ----no characters and has length 0
• Continuing this example, what do you think fruit[:] means? Try it and see.
• Strings Are Immutable(not change)
• It is tempting to use the [] operator on the left side of an assignment, ----- the
intention of changing a character in a string.
• For example:
• >>> greeting = 'Hello, world!'
• >>> greeting[0] = 'J'
• TypeError: 'str' object does not support item assignment
• The “object” in this case is the string and the “item” is the character you tried to assign.
• The reason for the error -----strings are immutable----- which means you can’t change an
existing string.
• The best -----create a new string -----variation on the original:
• >>> greeting = 'Hello, world!'
• >>> new_greeting = 'J' + greeting[1:]
• >>> new_greeting
• 'Jello, world!’
• This example concatenates a new first letter----- onto a slice of greeting.
• It has no effect------ on the original string.
• String Methods
• Strings -----provide methods------ that perform a variety of useful operations.
• A method is similar to a function—it takes arguments and returns a value—but the syntax is
different.
• For example----- the method upper -------takes a string and returns a new string with all uppercase
letters.
• Instead of the function syntax upper(word), it uses the method syntax [Link]():
• >>> word = 'banana'
• >>> new_word = [Link]()
• >>> new_word
• 'BANANA'
• The empty parentheses----- indicate------ that this method takes no arguments.
• A method call is called an invocation;
• In this case, we would say that we are invoking (call)----upper on word.
• There is a string method named--- “find”----- that is remarkably similar to the
function we wrote:
• >>> word = 'banana'
• >>> index = [Link]('a')
• >>> index
•1
• Actually---- the find method is------- more general than our function;
• it can find substrings,-----not just characters:
• >>> [Link]('na')
•2
• By default---- find starts at the beginning of the string, but it can take a second
arguement, the index where it should start:
• >>> [Link]('na', 3)
•4
• This is an example of an “optional argument”
• find ---- also take a third argument, the index where it should stop:
• >>> name = 'bob'
• >>> [Link]('b', 1, 2)
• -1
• This search fails ---because b does not appear----- in the index range from 1
to 2
• STRING MODULE:PYTHON ARRAY
• In Python----- the string module---- is not directly related to arrays.
• The string module----- provides a collection of constants and classes for dealing with
strings of characters.
• However------ if you're looking to work------ with arrays of characters (i.e., strings), you
would typically use the built-in list data type.
• Here's a brief overview of the string module and how you might use it alongside arrays:
• import string
• print([Link])
• print(string.ascii_letters)
• print([Link])
• my_string = "Hello, World!“
• char_array = list(my_string)
• print(char_array)