<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Strings
String is a Sequence
A string is a sequence of characters. You can access the characters one at a time with the bracket operator
Step1:  The second statement selects character number 1 from fruit and assigns it to letter.
The expression in brackets is called an index. The index indicates which character in the sequence you want (hence the name).
Step2:  For most people, the first letter of 'pinapple' is p, not i. But for computer scientists, the index is an offset from the beginning of the string, and the offset of the first letter is zero.
Step3:  So b is the 0th letter (“zero-eth”) of 'pinapple', a is the 1th letter (“one-eth”), and n is the 2th d(“two-eth”) letter.
You can use any expression, including variables and operators, as an index, but the value of the index has to be an integer. Otherwise you get
Step4:  The len function
The len is a built-in function that returns the number of characters in a string
Step5:  If you try to get the last letter in variable fruit, you should use n-1 for last item as in indexing, otherwise you will get an index out of range error.
Step6:  Alternatively, you can use negative indices, which count backward from the end of the string. The expression fruit[-1] yields the last letter, fruit[-2] yields the second to last, and so on.
Traversal with a for loop
We usually process string one character at a time to make most out of the computations. Going from the first character to the last one is called traversal. We can write a traversal using while loop but it's not really efficient in terms of time spend on writing the code
Step7:  This loop traverses the string and displays each letter on a line by itself. The loop condition is index &lt; len(fruit), so when index is equal to the length of the string, the condition is false, and the body of the loop is not executed. The last character accessed is the one with the index len(fruit)-1, which is the last character in the string.
Try Yourself!
Exercise
Step8:  Each time through the loop, the next character in the string is assigned to the variable char. The loop continues until no characters are left.
The following example shows how to use concatenation (string addition) and a for loop to generate an abecedarian series (that is, in alphabetical order). In Robert McCloskey’s book MakeWay for Ducklings, the names of the ducklings are Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack. This loop outputs these names in order
Step9:  Of course, that’s not quite right because “Ouack” and “Quack” are misspelled.
Try Yourself!
Exercise
Step10:  The operator [n
Step11:  If the first index is greater than or equal to the second the result is an empty string, represented by two quotation marks
Step12:  An empty string contains no characters and has length 0, but other than that, it is the same as any other string.
Try Yourself!
Exercise
Step13:  The “object” in this case is the string and the “item” is the character you tried to assign. For now, an object is the same thing as a value, but we will refine that definition later. An item is one of the values in a sequence.
The reason for the error is that strings are immutable, which means you can’t change an
existing string. The best you can do is create a new string that is a variation on the original. This example concatenates a new first letter onto a slice of greeting. It has no effect on the
original string
Step14:  Searching
What does the following function do?
Python
def find(word, letter)
Step15:  This form of dot notation specifies the name of the method, upper, and the name of the string to apply the method to, word. The empty parentheses indicate that this method takes no argument.
A method call is called an invocation; in this case, we would say that we are invoking upper on the word.
As it turns out, there is a string method named find that is remarkably similar to the function we wrote
Step16:  In this example, we invoke find on word and pass the letter we are looking for as a parameter. Actually, the find method is more general than our function; it can find substrings, not just characters
Step17:  It can take as a second argument the index where it should start
Step18:  And as a third argument the index where it should stop
Step19:  This search fails because b does not appear in the index range from 1 to 2 (not including 2).
Try Yourself!
Exercise1
Step20:  For example, the following function prints all the letters from word1 that also appear in word2
Step21:  String Comparison
The relational operators work on strings. To see if two strings are equal
Step22:  Other relational operations are useful for putting words in alphabetical order
<ASSISTANT_TASK:>
Python Code:
fruit = "pinapple"
letter = fruit[1]
Explanation: Strings
String is a Sequence
A string is a sequence of characters. You can access the characters one at a time with the bracket operator:
End of explanation
print(letter)
Explanation: The second statement selects character number 1 from fruit and assigns it to letter.
The expression in brackets is called an index. The index indicates which character in the sequence you want (hence the name).
End of explanation
letter = fruit[0]
print(letter)
Explanation: For most people, the first letter of 'pinapple' is p, not i. But for computer scientists, the index is an offset from the beginning of the string, and the offset of the first letter is zero.
End of explanation
letter = fruit[1.5]
Explanation: So b is the 0th letter (“zero-eth”) of 'pinapple', a is the 1th letter (“one-eth”), and n is the 2th d(“two-eth”) letter.
You can use any expression, including variables and operators, as an index, but the value of the index has to be an integer. Otherwise you get:
End of explanation
fruit = 'banana'
len(fruit)
Explanation: The len function
The len is a built-in function that returns the number of characters in a string:
End of explanation
length = len(fruit)
fruit[length]
fruit[length-1]
Explanation: If you try to get the last letter in variable fruit, you should use n-1 for last item as in indexing, otherwise you will get an index out of range error.
End of explanation
fruit = 'pinapple'
index = 0
while index < len(fruit):
    letter = fruit[index]
    print(letter)
    index = index + 1
Explanation: Alternatively, you can use negative indices, which count backward from the end of the string. The expression fruit[-1] yields the last letter, fruit[-2] yields the second to last, and so on.
Traversal with a for loop
We usually process string one character at a time to make most out of the computations. Going from the first character to the last one is called traversal. We can write a traversal using while loop but it's not really efficient in terms of time spend on writing the code:
End of explanation
for char in fruit:
    print(char)
Explanation: This loop traverses the string and displays each letter on a line by itself. The loop condition is index &lt; len(fruit), so when index is equal to the length of the string, the condition is false, and the body of the loop is not executed. The last character accessed is the one with the index len(fruit)-1, which is the last character in the string.
Try Yourself!
Exercise: Write a function that takes a string as an argument and displays the letters backward, one per line.
Another and more pythonic way of writing traversal is with for loops:
End of explanation
prefixes = 'JKLMNOPQ'
suffix = 'ack'
for letter in prefixes:
    print(letter + suffix)
Explanation: Each time through the loop, the next character in the string is assigned to the variable char. The loop continues until no characters are left.
The following example shows how to use concatenation (string addition) and a for loop to generate an abecedarian series (that is, in alphabetical order). In Robert McCloskey’s book MakeWay for Ducklings, the names of the ducklings are Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack. This loop outputs these names in order:
End of explanation
s = 'Monty Python'
print(s[0:5])
print(s[6:12])
Explanation: Of course, that’s not quite right because “Ouack” and “Quack” are misspelled.
Try Yourself!
Exercise: Modify the program to fix this error.
String Slices
A segment of a string is called a slice. Selecting a slice is similar to selecting a character:
End of explanation
fruit = 'banana'
fruit[:3]
fruit[3:]
Explanation: 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 counterintuitive, but it might help to imagine the indices pointing between the characters.
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:
End of explanation
fruit = 'banana'
fruit[3:3]
Explanation: If the first index is greater than or equal to the second the result is an empty string, represented by two quotation marks:
End of explanation
greeting = 'Hello, world!'
greeting[0] = 'J'
Explanation: An empty string contains no characters and has length 0, but other than that, it is the same as any other string.
Try Yourself!
Exercise: Given that fruit is a string, what does fruit[:] mean?
Strings are immutable
It is tempting to use the [] operator on the left side of an assignment, with the intention of changing a character in a string. For example:
End of explanation
greeting = 'Hello, world!'
new_greeting = 'J' + greeting[1:]
print(new_greeting)
Explanation: The “object” in this case is the string and the “item” is the character you tried to assign. For now, an object is the same thing as a value, but we will refine that definition later. An item is one of the values in a sequence.
The reason for the error is that strings are immutable, which means you can’t change an
existing string. The best you can do is create a new string that is a variation on the original. This example concatenates a new first letter onto a slice of greeting. It has no effect on the
original string:
End of explanation
word = 'banana'
word.upper()
Explanation: Searching
What does the following function do?
Python
def find(word, letter):
    index = 0
    while index &lt; len(word):
        if word[index] == letter:
            return index
        index = index + 1
    return -1
In a sense, find is the opposite of the [] operator. Instead of taking an index and extracting the corresponding character, it takes a character and finds the index where that character appears. If the character is not found, the function returns -1.
This is the first example we have seen of a return statement inside a loop. If word[index] == letter, the function breaks out of the loop and returns immediately.
If the character doesn’t appear in the string, the program exits the loop normally and returns -1. 
This pattern of computation—traversing a sequence and returning when we find what we are looking for—is called a search.
Try Yourself!
Exercise: Modify find so that it has a third parameter, the index in word where it should start looking.
Looping and Counting
The following program counts the number of times the letter a appears in a string:
Python
word = 'banana'
count = 0
for letter in word:
    if letter == 'a':
        count = count + 1
print(count)
This program demonstrates another pattern of computation called a counter. The variable count is initialized to 0 and then incremented each time an a is found. When the loop exits, count contains the result—the total number of a’s.
Try Yourself!
Exercise1: Encapsulate this code in a function named count, and generalize it so that it accepts the string and the letter as arguments.
Exercise2: Rewrite this function so that instead of traversing the string, it uses the three-parameter version of find from the previous section.
String Methods
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 word.upper().
End of explanation
word = 'banana'
word.find('a')
Explanation: This form of dot notation specifies the name of the method, upper, and the name of the string to apply the method to, word. The empty parentheses indicate that this method takes no argument.
A method call is called an invocation; in this case, we would say that we are invoking upper on the word.
As it turns out, there is a string method named find that is remarkably similar to the function we wrote:
End of explanation
word.find('na')
Explanation: In this example, we invoke find on word and pass the letter we are looking for as a parameter. Actually, the find method is more general than our function; it can find substrings, not just characters:
End of explanation
word.find('na', 3)
Explanation: It can take as a second argument the index where it should start:
End of explanation
name = 'bob'
name.find('b', 1, 2)
Explanation: And as a third argument the index where it should stop:
End of explanation
'a' in 'Banana'
'seed' in 'banana'
Explanation: This search fails because b does not appear in the index range from 1 to 2 (not including 2).
Try Yourself!
Exercise1: There is a string method called count that is similar to the function in the previous exercise. Read the documentation of this method and write an invocation that counts the number of as in 'banana'.
The in Operator
The word in is a boolean operator that takes two strings and returns True if the first appears as a substring in the second:
End of explanation
def in_both(word1, word2):
    for letter in word1:
        if letter in word2:
            print(letter)
in_both("apples", "oranges")
Explanation: For example, the following function prints all the letters from word1 that also appear in word2:
Python
def in_both(word1, word2):
    for letter in word1:
        if letter in word2:
            print(letter)
With well-chosen variable names, Python sometimes reads like English. You could read this loop, “for (each) letter in (the first) word, if (the) letter (appears) in (the second) word, print (the) letter.”
Here’s what you get if you compare apples and oranges:
End of explanation
if word == 'banana':
    print('All right, bananas.')
Explanation: String Comparison
The relational operators work on strings. To see if two strings are equal:
End of explanation
if word < 'banana':
    print('Your word,' + word + ', comes before banana.')
elif word > 'banana':
    print('Your word,' + word + ', comes after banana.')
else:
    print('All right, bananas.')
Explanation: Other relational operations are useful for putting words in alphabetical order:
End of explanation
<END_TASK>