Key Concepts

Review core concepts you need to learn to master this subject

Strings

In computer science, sequences of characters are referred to as strings. Strings can be any length and can include any character such as letters, numbers, symbols, and whitespace (spaces, tabs, new lines).

Introduction to Strings
Lesson 1 of 2
  1. 1
    Words and sentences are fundamental to how we communicate, so it follows that we’d want our computers to be able to work with words and sentences as well. In Python, the way we store something li…
  2. 2
    A string can be thought of as a list of characters. Like any other list, each character in a string has an index. Consider the string: favorite_fruit = “blueberry” We can select specific le…
  3. 3
    Not only can we select a single character from a string, but we can also select entire chunks of characters from a string. We can do this with the following syntax: string[first_index:last_index] …
  4. 4
    We can also concatenate, or combine, two existing strings together into a new string. Consider the following two strings: fruit_prefix = “blue” fruit_suffix = “berries” We can create a new str…
  5. 5
    Python comes with some built-in functions for working with strings. One of the most commonly used of these functions is len(). len() returns the number of characters in a string: favorite_fruit = …
  6. 6
    In the previous exercise, we used len() to get a slice of characters at the end of a string. There’s a much easier way to do this — we can use negative indices! Negative indices count backwa…
  7. 7
    So far in this lesson, we’ve been selecting characters from strings, slicing strings, and concatenating strings. Each time we perform one of these operations we are creating an entirely new string….
  8. 8
    Occasionally when working with strings, you’ll find that you want to include characters that already have a special meaning in python. For example let’s say I create the string favorite_fruit_con…
  9. 9
    Now you know enough about strings that we can start doing the really fun stuff! Because strings are lists, that means we can iterate through a string using for or while loops. This opens up a whol…
  10. 10
    Now that we are iterating through strings, we can really explore the potential of strings. When we iterate through a string we do something with each character. By including conditional statement…
  11. 11
    There’s an even easier way than iterating through the entire string to determine if a character is in a string. We can do this type of check more efficiently using in. in checks if one string is pa…
  12. 12
    Great work! I hope you are now starting to see the potential of strings and how they can be used to solve a huge variety of problems. In this lesson you learned: - A string is a list of characters…