Modules -> Data Structures -> Strings -> A string is a sequence

A string is a sequence

A string is a 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 is 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, the first character's index is 0, not 1.

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

Exercises


Assign a string with more than 5 characters to b.


Print third character of it.




Define a function that takes a string and returns its first character.




Define a function that takes a string and an integer, word and n, and returns character of word with index n.




Define a function that takes a string and returns True if its first character is 'b'.




Define a function that takes a string, a character and an integer, word, char and i, and returns True if character of word with index i is equal to char.



def return_char(s, n):
    return s[n]

Given the above script, what are the results of the following expressions:

return_char('llukar', 0):
return_char('xerxe', 3):
return_char('carralluke', 1):


def is_char(word, ch, i):
    return word[i] == ch

Given the above script, what are the results of the following expressions:

is_char("llukar", "u", 2):
is_char("xerxe", "x", 3):
is_char("carralluke", "r", 3):


Complete execution flow of the following program

Misses: 0
def return_char(s, n):
    return s[n]

return_char('llapushnik', 4)
return_char('llaushe', 2 + 4)


Complete execution flow of the following program

Misses: 0
def is_char(word, ch, i):
    return word[i] == ch

is_char('llapushnik', 'u', 4)
is_char('llaushe', 'e', 5)



Exercises


Assign a string with more thane 5 characters to a.


Print first character of it.




Define a function that takes a string and returns its second character.




Define a function that takes a string and an integer, word and n, and returns character of word with index n + 1.




Define a function that takes a string and returns True if its third character is 'q'.




Define a function that takes a string and a character, word and char, and returns True if fourth character of word is equal to char.



def return_char(s, n):
    return s[n]

Given the above script, what are the results of the following expressions:

return_char('llukar', 2):
return_char('xerxe', 0):
return_char('carralluke', 3):


def is_char(word, ch, i):
    return word[i] == ch

Given the above script, what are the results of the following expressions:

is_char("llukar", "u", 3):
is_char("xerxe", "x", 0):
is_char("carralluke", "c", 1):


Complete execution flow of the following program

Misses: 0
def return_char(s, n):
    return s[n]

return_char('poroj', 2)
return_char('mujdin', 1)


Complete execution flow of the following program

Misses: 0
def is_char(word, ch, i):
    return word[i] == ch

is_char('poroj', 'o', 1)
is_char('mujdin', 'd', 3)