# Peer Instruction: Strings Multiple Choice QuestionsΒΆ

- kings
- Try again. s[i:j] extracts characters beginning at s[i] and ending about not including s[j]. And the first index in a string is index 0.
- king
- Try again. s[i:j] extracts characters beginning at s[i] and ending about not including s[j]. And the first index in a string is index 0. Index -1 is the right 1 character.
- Viking
- Correct! s[i:j] extracts characters beginning at s[i] and ending about not including s[j].
- Vikings
- Try again. s[i:j] extracts characters beginning at s[i] and ending about not including s[j]. Index -1 is the right 1 character. So 's' is not included.
- ikings
- Try again. s[i:j] extracts characters beginning at s[i] and ending about not including s[j]. And the first index in a string is index 0.

11-9-1: What does the following code print?

```
game = 'Lost Vikings'
print(game[5:-1])
```

- st V
- Correct! s[i:j] extracts characters beginning at s[i] and ending about not including s[j]. s[2] is the third character from left, and s[-6] is the sixth character from right. s[2] = 's' and s[-6] = 'i'.
- ost V
- Try again. s[i:j] extracts characters beginning at s[i] and ending about not including s[j]. s[2] is the third character from left. s[2] = 's'.
- iking
- Try again. s[i:j] extracts characters beginning at s[i] and ending about not including s[j]. s[2] is the third character from left, and s[6] is the sixth character from right. s[2] = 's' and s[-6] = 'i'.
- st Vi
- Try again. s[i:j] extracts characters beginning at s[i] and ending about not including s[j]. s[-6] is the sixth character from right. s[-6] = 'i'.
- Viking
- Try again. s[i:j] extracts characters beginning at s[i] and ending about not including s[j]. s[2] is the third character from left, and s[-6] is the sixth character from right. s[2] = 's' and s[-6] = 'i'.

11-9-2: What does the following code print?

```
game = 'Lost Vikings'
print(game[2:-6])
```

- ost Vikings
- Try again. s[i:j] extracts characters beginning at s[i] and ending about not including s[j]. s[-6] is the sixth character from right, and s[-1] is the first character from right. There is no index 0 when counting from right to left.
- ost Viking
- Try again. s[i:j] extracts characters beginning at s[i] and ending about not including s[j]. s[-6] is the sixth character from right, and s[-1] is the first character from right. There is no index 0 when counting from right to left.
- ikings
- Try again. s[i:j] extracts characters beginning at s[i] and ending about not including s[j]. s[-1] is the first character from right. There is no index 0 when counting from right to left.
- iking
- Correct! s[i:j] extracts characters beginning at s[i] and ending about not including s[j]. We can use negative indices in the slice syntax as well.
- Vikings
- Try again. s[i:j] extracts characters beginning at s[i] and ending about not including s[j]. s[-1] is the first character from right. There is no index 0 when counting from right to left.

11-9-3: What does the following code print?

```
game = 'Lost Vikings'
print(game[-6:-1])
```

- 11
- Try again. s.replace(old, new): return s but with all occurrences of old replaced by new. The new s = 'Miaiaauga'. And the length of new s is 9.
- ss
- Try again. t is the length of the new string because it equals to len(s).
- 10
- Try again. s.replace(old, new): return s but with all occurrences of old replaced by new. The new s = 'Miaiaauga'. And the length of new s is 9.
- Miaaiaaauga'
- Try again. t is the length of the new string because it equals to len(s).
- None of the above
- Correct! s.replace(old, new): return s but with all occurrences of old replaced by new. The new s = 'Miaiaauga'. And the length of new s is 9.

11-9-4: What does the following code print?

```
s = 'Mississauga'
t = len(s.replace('ss', 'a'))
print(t)
```

- 'xxcavexx'
- Correct! In string.center(length, character), the length is the length of the returned string and the character to fill the missing space on each side.
- ' cave '
- Try again. In string.center(length, character), the character to fill the missing space on each side. Here the character is 'x'.
- 'xxxxcavexxxx'
- Try again. In string.center(length, character), the length is the length of the returned string. The length here should be 8 rather than 12.
- ' cave '
- Try again. In string.center(length, character), the length is the length of the returned string and the character to fill the missing space on each side. The length here should be 8 rather than 12 and the filling character is 'x'.

11-9-5: What string is produced by the following code?

```
'cave'.center(8, 'x')
```

- 'a'
- Try again. ord converts characters to codes and chr converts codes to characters. ord('a') = 97, ord('z') = 122, ord(ch) = 119, val = 99. Therefore, mystery = 'c'.
- 'b'
- Try again. ord converts characters to codes and chr converts codes to characters. ord('a') = 97, ord('z') = 122, ord(ch) = 119, val = 99. Therefore, mystery = 'c'.
- 'c'
- Correct! ord converts characters to codes and chr converts codes to characters. ord('a') = 97, ord('z') = 122, ord(ch) = 119, val = 99. Therefore, mystery = 'c'.
- 'd'
- Try again. ord converts characters to codes and chr converts codes to characters. ord('a') = 97, ord('z') = 122, ord(ch) = 119, val = 99. Therefore, mystery = 'c'.
- 'e'

11-9-6: What is the value of `mystery`

?

```
ch = 'w'
val = ord('a') + 6 - (ord('z') - ord(ch) + 1)
mystery = chr(val)
```

- 'a'
- Try again. ord converts characters to codes and chr converts codes to characters. ord('a') = 97, ord('z') = 122, ord(ch) = 128, val = 98. Therefore, mystery = 'b'.
- 'b'
- Correct! ord converts characters to codes and chr converts codes to characters. ord('a') = 97, ord('z') = 122, ord(ch) = 128, val = 98. Therefore, mystery = 'b'.
- 'c'
- Try again. ord converts characters to codes and chr converts codes to characters. ord('a') = 97, ord('z') = 122, ord(ch) = 128, val = 98. Therefore, mystery = 'b'
- 'd'
- Try again. ord converts characters to codes and chr converts codes to characters. ord('a') = 97, ord('z') = 122, ord(ch) = 128, val = 98. Therefore, mystery = 'b'
- 'e'
- Try again. ord converts characters to codes and chr converts codes to characters. ord('a') = 97, ord('z') = 122, ord(ch) = 128, val = 98. Therefore, mystery = 'b'

11-9-7: What is the value of `mystery`

?

```
ch = 'x'
val = ord('a') + 4 - (ord('z') - ord(ch) + 1)
mystery = chr(val)
```

- a{0}b
- Try again. The format(4) method formats the specified value(4) and insert them inside the string's placeholder. The placeholder is defined using curly brackets: {}.
- ab
- Try again. s2 = a{0}b. The format(4) method formats the specified value(4) and insert them inside the string's placeholder. The placeholder is defined using curly brackets: {}.
- a4b
- Correct! The format(4) method formats the specified value(4) and insert them inside the string's placeholder.
- The code does not run
- Try again. The code can run. {n} inserts the argument, thereefore s2 = a{0}b. The format(4) method formats the specified value(4) and insert them inside the string's placeholder. The placeholder is defined using curly brackets: {}.

11-9-8: What does the following code print?

```
s1 = '0'
s2 = 'a{' + s1 + '}b'
print(s2.format(4))
```

You have attempted of activities on this page