Concepts / Introduction to Sequences

Introduction to Sequences

A string is a sequence of characters . Strings are basically just a bunch of words.

  • Programming

What Is a Sequence?

A sequence is an ordered collection of items arranged in a specific order. In programming, the most common sequence you will encounter is a string. A string is a sequence of characters—meaning it is a collection of individual letters, digits, symbols, and spaces all arranged one after another in a particular order. When we say 'sequence,' we emphasize that position matters. The first character is different from the second character, and rearranging them creates a different string entirely. For example, 'cat' and 'tac' are both sequences of the same three characters, but they are different strings because the order is different.

Think of a sequence like a row of mailboxes on a street. Each mailbox holds one piece of mail (a character), and each mailbox has a specific position—first, second, third, and so on. If you want to retrieve the mail from the third mailbox, you need to know it is the third one. If the mailboxes were rearranged, you would get different mail. Similarly, in the string 'hello', the 'h' is in the first position, 'e' is in the second, 'l' is in the third, 'l' is in the fourth, and 'o' is in the fifth. The order is fixed, and each character occupies a specific position.

How Characters Are Positioned in a String

Every character in a string occupies a specific position, and we can refer to that position using an index. Understanding how characters are arranged and how to reference them is fundamental to working with sequences. Each position in a string has a numerical index that tells us exactly where that character is located.

Pindex 0yindex 1tindex 2hindex 3oindex 4nindex 5
How are individual characters arranged and positioned within a string, and how do I refer to each one?

In most programming languages, including Python, indexing starts at 0, not 1. This means the first character is at index 0, the second character is at index 1, and so on. This is called zero-based indexing. In the string 'Python', the 'P' is at index 0, 'y' is at index 1, 't' is at index 2, 'h' is at index 3, 'o' is at index 4, and 'n' is at index 5.

Strings as Collections of Words

While a string is fundamentally a sequence of characters, we often think of strings as containing words. It is important to understand the distinction: a string is always a sequence of individual characters (including spaces), but we can interpret or group those characters as words when it serves our purpose. For example, the string 'What is your name?' is a sequence of 18 characters (including spaces and punctuation), but we naturally read it as five words. Understanding this dual nature helps you work flexibly with text data.

Consider the string 'Hello World'. At the character level, it is a sequence of 11 items: 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'. The space character (at index 5) is just as much a part of the sequence as any letter. At the word level, we recognize two words: 'Hello' and 'World'. Both perspectives are valid; the choice depends on what task you are performing. If you need to count characters, you work at the character level. If you need to process individual words, you might group characters into words.

Why Order Matters in a Sequence

The defining feature of a sequence is that order is significant. If you rearrange the characters in a sequence, you get a different sequence. This is not merely a technical detail—it is fundamental to how sequences work. The sequence 'cat' is different from 'act' or 'tac' because the characters appear in different positions. In many programming tasks, you will need to preserve or respect the order of elements in a sequence because changing the order changes the meaning or value of the data.

What do you think happens?

If you have the string 'abc' and you rearrange it to 'bca', are these the same sequence?

  • Yes, they contain the same characters so they are the same.
  • No, the order is different so they are different sequences.
Reveal answer

Answer: No, the order is different so they are different sequences.

A sequence is defined not just by which items it contains, but by the order in which they appear. 'abc' and 'bca' both contain the letters a, b, and c, but because they appear in different positions, these are two distinct sequences. This is why order is a core property of sequences.

Escape Sequences and Special Characters

Sometimes you need to include special characters in a string that are not easily typed or that have special meaning. To do this, you use escape sequences. An escape sequence is a combination of characters that represents a single character in the string. The most common escape sequences are the newline character (represented as \n) and the tab character (represented as \t). Another important one is the backslash itself (represented as \\), which you use when you need to include a literal backslash in your string.

When you write \n in a string, it does not appear as two characters; instead, it represents a single newline character that moves the text to the next line. Similarly, \t represents a single tab character that creates horizontal spacing. These escape sequences are part of the string sequence itself—they are how you represent characters that cannot be easily typed or displayed.

If you want to create a two-line string, you can use the escape sequence \n. For example, the string 'Hello\nWorld' is a sequence of 11 characters: 'H', 'e', 'l', 'l', 'o', (newline), 'W', 'o', 'r', 'l', 'd'. When displayed, this string appears on two lines. You can also use triple-quoted strings to write multi-line text directly, but escape sequences give you precise control over where line breaks and tabs appear.

Common Mistakes with Sequences

  • Forgetting that indexing starts at 0, not 1

    Most programming languages use zero-based indexing. The first position is always 0. If you try to access index 1 thinking it is the first character, you will actually get the second character.

    Fix: Always remember: index 0 is the first character, index 1 is the second, and so on.

  • Treating a string as a single unit rather than a sequence

    A string is a sequence, which means you can access and work with individual characters by their position. Understanding this opens up many possibilities for manipulating text.

    Fix: Remember that a string is made up of individual characters, each with its own index, and you can refer to them individually.

  • Confusing escape sequences with literal characters

    Escape sequences like \n are interpreted by the programming language as special characters (in this case, a newline), not as two literal characters.

    Fix: Understand that \n, \t, and \\ are escape sequences that represent single special characters, not multiple characters.

Practicing with Sequences

EASY

Consider the string 'Programming'. Answer the following questions: (1) How many characters are in this string? (2) What character is at index 0? (3) What character is at index 5? (4) If you wanted to display this string on two lines with 'Program' on the first line and 'ming' on the second, what escape sequence would you use?

Hints
  • Count every character, including letters only—no spaces in this example.
  • Remember that indexing starts at 0.
  • The escape sequence for a newline is \n.
EASY

Write out the string 'Hello, World!' and label each character with its index (0 through 12). Then identify which character is at index 6 and which index corresponds to the space character.

Hints
  • Include punctuation and spaces in your count.
  • The comma is a character with its own index.
  • The space is at index 6.

Key Takeaways

A sequence is an ordered collection where position matters. A string is the most common sequence in programming—it is a collection of characters arranged in a specific order. Each character occupies a position identified by an index, starting from 0 for the first character. Understanding that a string is a sequence of characters (not just a blob of text) is essential for manipulating and working with text data effectively. Escape sequences like \n and \t allow you to include special characters in strings. Finally, remember that order is fundamental to sequences: rearranging characters creates a different sequence with different meaning.

Key Takeaways

  • A sequence is an ordered collection of items where position and order are significant.
  • A string is a sequence of characters, with each character occupying a specific position identified by an index starting from 0.
  • Individual characters in a string can be referenced by their index position.
  • Escape sequences like \n (newline) and \t (tab) allow you to include special characters in strings.
  • Understanding strings as sequences of characters (rather than just words) enables more flexible and powerful text manipulation.