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).

Indexing and Slicing Strings

Python strings can be indexed using the same notation as lists, since strings are lists of characters. A single character can be accessed with bracket notation ([index]), or a substring can be accessed using slicing ([start:end]).

Indexing with negative numbers counts from the end of the string.

String concatenation

To combine the content of two strings into a single string, Python provides the + operator. This process of joining strings is called concatenation.

`len()’ Built-in Function in Python

In Python, the built in function len() calculates the length of objects. It can be used to compute the length of strings, lists, sets, and other countable objects!

IndexError

When indexing into a string in Python, if you try to access an index that doesn’t exist, an IndexError is generated. For example, the following code would create an IndexError:

Immutable strings

Strings are immutable in Python. This means that once a string has been defined, it can’t be changed.

There are no mutating methods for strings. This is unlike data types like lists, which can be modified once they are created.

Escaping characters in a string

Backslashes (\) are used to escape characters in a Python string.

For instance, to print a string with quotation marks, the given code snippet can be used.

Iterate String

To iterate through a string in Python, “for…in” notation is used.

The Python in syntax

In Python, the in syntax is used to determine if a letter or a substring exists in a string. It returns True if a match is found, otherwise False is returned.

Lower

In Python, the string method .lower() returns a string with all uppercase characters converted into lowercase. .lower() does not take any input arguments.

Python string method .upper()

The Python string method .upper() returns the string with all lowercase characters converted to uppercase.

Python string method .title()

The Python string method .title() returns the string in title case. With title case, the first character of each word is capitalized while the rest of the characters are lowercase.

Python string .split() method

The Python string method .split() will split a string into a list of items.

If an argument is passed to the method, that value is used as the delimiter on which to split the string.

If no argument is passed, the default behavior is to split on whitespace.

The .join() String Method in Python

In Python, the .join() method can be used to concatenate a list of strings together to create a new string joined with the desired delimiter.

The .join() method is run on the delimiter and the array of strings to be concatenated together is passed in as an argument.

The .strip() String Method in Python

The string method .strip() can be used to remove characters from the beginning and end of a string.

A string argument can be passed to the method, specifying the set of characters to be stripped. With no arguments to the method, whitespace is removed.

String replace

The .replace() method is used to replace the occurence of the first argument with the second argument within the string.

The first argument is the old substring to be replaced, and the second argument is the new substring that will replace every occurence of the first one within the string.

Python string method .find()

The Python string method .find() returns the index of the first occurrence of the string passed as the argument. It returns -1 if no occurrence is found.

Python String .format()

The Python string method .format() replaces empty brace ({}) placeholders in the string with its arguments.

If keywords are specified within the placeholders, they are replaced with the corresponding named arguments to the method.

Chevron Left Icon
Introduction to Strings
Lesson 1 of 3
Chevron Right Icon
  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 let…
  3. 3
    Not only can we select a single character from a string, we can select entire chunks of characters from a string. We can do this with the following syntax: string_name[first_index:last_index] Th…
  4. 4
    You can also concatenate two existing strings together into a new string. Consider the following two strings. fruit_prefix = “blue” fruit_suffix = “berries” We can create a new string by concate…
  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_frui…
  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 backward from…
  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…
  1. 1
    Do you have a gigantic string that you need to parse for information? Do you need to sanitize a users input to work in a function? Do you need to be able to generate outputs with variable values? A…
  2. 2
    There are three string methods that can change the casing of a string. These are .lower(), .upper(), and .title(). - .lower() returns the string with all lowercase characters. - .upper() returns t…
  3. 3
    .upper(), .lower(), and .title() all are performed on an existing string and produce a string in return. Let’s take a look at a string method that returns a different object entirely! .split() is …
  4. 4
    If we provide an argument for .split() we can dictate the character we want our string to be split on. This argument should be provided as a string itself. Consider the following example: >>> g…
  5. 5
    We can also split strings using escape sequences. Escape sequences are used to indicate that we want to split by something in a string that is not necessarily a character. The two escape sequen…
  6. 6
    Now that you’ve learned to break strings apart using .split(), let’s learn to put them back together using .join(). .join() is essentially the opposite of .split(), it joins a list of strings tog…
  7. 7
    In the last exercise, we joined together a list of words using a space as the delimiter to create a sentence. In fact, you can use any string as a delimiter to join together a list of strings. For …
  8. 8
    When working with strings that come from real data, you will often find that the strings aren’t super clean. You’ll find lots of extra whitespace, unnecessary linebreaks, and rogue tabs. Python p…
  9. 9
    The next string method we will cover is .replace(). Replace takes two arguments and replaces all instances of the first argument in a string with the second argument. The syntax is as follows str…
  10. 10
    Another interesting string method is .find(). .find() takes a string as an argument and searching the string it was run on for that string. It then returns the first index value where that string…
  11. 11
    Python also provides a handy string method for including variables in strings. This method is .format(). .format() takes variables as an argument and includes them in the string that it is run on. …
  12. 12
    .format() can be made even more legible for other people reading your code by including keywords. Previously with .format(), you had to make sure that your variables appeared as arguments in the …
  13. 13
    Excellent work! This lesson has shown you the vast variety of string methods and their power. Whatever the problem you are trying to solve, if you are working with strings then string methods are l…
  1. 1
    This lesson will help you review Python functions by providing some challenge exercises involving Strings. As a refresher, function syntax looks like this: def some_function(some_input1, some_inp…
  2. 2
    unique_english_letters(word)
  3. 3
    count_char_x()
  4. 4
    count_multi_char_x()
  5. 5
    substring_between_letters()
  6. 6
    x_length_words()
  7. 7
    check_for_name()
  8. 8
    every_other_letter()
  9. 9
    reverse_string()
  10. 10
    make_spoonerism()
  11. 11
    add_exclamation()

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo