Character strings

The string as an indexed sequence of characters: length, access, slicing, concatenation, immutability, traversal, lexicographic order and common operations (case, search, replacement, splitting, joining).

  • 26 cards
  • 6 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. The numbers
  5. Scenarios
  6. Understand

A few cards from the deck

  • String: what is it?

    Show the answer

    An ordered sequence of characters, identified by their position like the cells of an array, used to represent text.

    len('hello') is 5, 'hello'[0] is 'h' and 'hello'[-1] is 'o'. A single character is a string of length 1 in Python.

  • What does this program print?

    ```python
    s = "informatique"
    print(s[2:7:2])
    `

    • fra
    • forma
    • omt
    • nom
    Show the answer

    fra

    The indices taken are 2, 4 and 6: f, r, a (7 is excluded). forma ignores the step, omt starts at index 3, nom at index 1.

  • Joining two strings end to end is called …; in Python, it is written with the … operator.

    Show the answer

    Joining two strings end to end is called concatenation; in Python, it is written with the + operator.

    'ab' + 'cd' gives 'abcd', but 'ab' + 1 raises a TypeError: convert with str(1). The * operator repeats: 'ab' * 3 gives 'ababab'.

  • Slice: what is it?

    Show the answer

    A contiguous part of a string, given by a start index, an excluded end index and, optionally, a step.

    s[2:5] takes indices 2, 3 and 4. An omitted bound goes all the way: s[:3] for the first three characters, s[-3:] for the last three, s[::-1] for the reversed string.