Get startedGet started for free

Working with strings

1. Working with strings

Welcome back! Previously, we introduced strings. Now let's look at this data type in more detail!

2. Strings are everywhere!

Strings are used extensively in software, so we must know how to work with them! For example, they are used for gathering user input in profiles and search engines or when working with large language models.

3. Python knows single and double quotes

As we know, Python strings can be created using single or double quotes, like so. One reason for choosing double quotes is if we want to use an apostrophe because this is the same character as a single quote.

4. Advantages of double quotes

If we try to include an apostrophe within a string using single quotes, Python will think the apostrophe is the end of the string and won't know what to do with the rest of the text, showing a syntax error as an output. Using double quotes, we can save and then print out the variable without any errors.

5. Methods

Python offers built-in methods for working with strings. A method is a function that is only available to a particular data type, for example, strings. The generic syntax is to place a period, or dot, after the variable we are working with, then call the method by writing its name and ending with parentheses.

6. Replacing parts of strings

We can replace parts of the string with values of our choice using the dot-replace method. As we are updating the variable we assign it by writing my-text equals, then write my-text again, followed by calling the dot-replace method. We provide two things inside the parentheses: first, the text to be replaced, and second, the text to change it to. This updates my_text by changing George to John. Printing the variable shows it has been changed. Common use cases include reformatting to ensure consistency or to fix or remove typos.

7. Changing case

We can also convert between upper and lowercase, which helps if we need consistent formats for record keeping and analysis. If we want to convert our current-top-album variable to lowercase, we use the dot-lower method. Unlike dot-replace, we don't need to provide anything inside parentheses. Likewise, to convert to all uppercase, we can use the string-dot-upper method.

8. Sentences and paragraphs

So far we haven't worked with long blocks of text. Here, we store the opening sentence from George Orwell's book 1984, which just fits on a single line. When working with more text we will need to wrap it on to additional lines.

9. Multi-line strings

If we want to spread text over multiple lines, then the convention is to use three sets of double quotes. As an example, we store the opening sentence from the first Harry Potter book. When we use three sets of quotes like this, it is generally referred to as a multi-line string. The benefit of this approach is that it is easily readable. While there are ways to spread text across multiple lines using a single set of quotes, this requires the use of special characters known as escape characters, which we won't cover in this course. Given that multi-line strings store blocks of text, they are commonly used for customer reviews but are also often used to describe what a function does.

10. String cheat sheet

This table summarizes the syntax we've seen to create or modify strings. Python has lots of additional functionality, which you can learn more about in the documentation linked.

11. Let's practice!

Now let's string some exercises together!