Get AI summaries of any video or article — Sign up free
Python Tutorial for Beginners 2: Strings - Working with Textual Data thumbnail

Python Tutorial for Beginners 2: Strings - Working with Textual Data

Corey Schafer·
5 min read

Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

Python strings represent textual data and are typically stored in variables for reuse.

Briefing

Python strings are the core way Python represents textual data, and the practical skill is learning how to define them safely, inspect them, and transform them. The tutorial starts with a simple “hello world” example, then shows how storing that text in a variable (like message) lets the same text be printed, reused, and manipulated throughout a program. It also emphasizes Python’s whitespace-based syntax—no semicolons—while reinforcing naming conventions such as lowercase variable names and underscores for multi-word identifiers.

A major early focus is how quotes work inside strings. Single quotes and double quotes both work for string boundaries, but a string containing an apostrophe (e.g., Bobby’s World) will break if the same quote type is used to delimit the string. The fix can be done in two common ways: escaping the internal quote with a backslash, or choosing the opposite quote type for the outside delimiter. For multi-line text, the tutorial demonstrates triple quotes (either single or double) so the string can span multiple lines without manual newline characters.

From there, the lesson shifts to treating strings as sequences of characters. The built-in len function returns the number of characters in a string, and indexing uses zero-based positions—so the last character sits at index length minus one. Attempting to access an out-of-range index triggers an IndexError. The tutorial then introduces slicing to extract portions of text using start:stop notation, where the start index is inclusive and the stop index is exclusive. Examples show how to slice out “hello” (0:5) and “world” (6:), including the shorthand behavior when the start or stop side is omitted.

Once comfortable with character access, the tutorial moves into string methods—operations available on string objects. It demonstrates case conversion with .lower() and .upper(), counting occurrences with .count() (counting substrings like “hello” or single characters like “L”), and locating text with .find(), including the convention of returning -1 when a substring doesn’t exist. It also covers .replace(), highlighting a key behavior: string methods typically don’t modify the original string in place; instead, they return a new string. To apply replacements, the returned value must be assigned back to a variable (either a new one or the same one).

Finally, the tutorial covers building strings from multiple parts. It shows concatenation with the + operator, including the common pitfall of forgetting spaces, and then contrasts that with formatted strings. The .format() method uses curly-brace placeholders, while f strings (available in Python 3.6+) allow embedding variables directly inside the string and even running expressions like name.upper() within the placeholder. The closing section provides practical tooling: dir() lists available attributes and methods for a value, and help() (used with the str class or a specific method) gives descriptions and argument information so learners can quickly look up what a string method does without searching online. The takeaway is that once strings are understood as character sequences plus a toolbox of methods and formatting options, most text-processing tasks become straightforward in Python.

Cornell Notes

Strings are Python’s representation for textual data, and they can be stored in variables and reused throughout a program. The tutorial shows how to define strings safely using single quotes, double quotes, escaping, and triple quotes for multi-line text. It then treats strings as character sequences: len() measures length, indexing uses zero-based positions, and slicing with start:stop extracts ranges where the start is inclusive and the stop is exclusive. A set of string methods—.lower(), .upper(), .count(), .find(), and .replace()—enables common transformations and searches, with .replace() returning a new string rather than modifying in place. Finally, it demonstrates concatenation, .format(), and f strings for building messages, plus dir() and help() for discovering and understanding available methods.

Why can a string like 'Bobby's World' fail in Python, and what are the standard fixes?

A failure happens when the same quote character is used both to delimit the string and to appear inside the text. For example, using single quotes around Bobby's World makes Python treat the apostrophe in Bobby’s as the end of the string. Two fixes are demonstrated: (1) escape the internal apostrophe with a backslash (Bobby\'s World), or (2) use double quotes around the string so the internal single quote doesn’t terminate it ("Bobby's World").

How do indexing and len() work together for strings?

len(message) returns the total number of characters. Indexing starts at 0, so the first character is at index 0 and the last character is at index len(message) - 1. If an index outside that range is accessed (like index 11 for a 11-character string), Python raises an IndexError.

What does slicing mean in Python, and why is the stop index exclusive?

Slicing uses start:stop to extract a portion of a string. The start index is inclusive, meaning it’s included in the result, while the stop index is exclusive, meaning it’s not included. For example, message[0:5] returns the characters from index 0 up to but not including index 5, producing "hello" from "hello world". Omitting start defaults to the beginning ([:5] style), and omitting stop defaults to the end ([6:] style).

What’s the difference between string methods that transform text and operations that modify in place?

String methods like .lower(), .upper(), and .replace() return new string values. The tutorial shows that calling message.replace('world','universe') doesn’t change message automatically; the replacement result must be assigned to a variable (e.g., new_message = message.replace(...)) or reassigned back to message (message = message.replace(...)).

When should a learner prefer f strings over .format() or concatenation?

Concatenation with + works but can become error-prone in longer expressions, especially around spaces and punctuation. .format() improves readability by using placeholders like {} and then supplying values with .format(greeting, name). f strings (Python 3.6+) simplify this further by embedding variables directly inside the string (e.g., f"{greeting}, {name}. Welcome!") and allow inline expressions such as {name.upper()}.

How can a learner discover what string methods exist and what they do?

Use dir(variable) to list available attributes and methods for a specific value, such as a string variable. For descriptions and argument details, use help(str) to view documentation for the string class, or use help on a specific method (like help(str.lower) or the method name as shown) to get targeted information about what it does and what parameters it takes.

Review Questions

  1. If a string has length 11, what are the valid index values, and what happens if you try to access index 11?
  2. Given message = "hello world", what does message[0:5] return, and why doesn’t message[0:5] include the character at index 5?
  3. What must be done after calling message.replace('world','universe') to actually see the replacement in the output?

Key Points

  1. 1

    Python strings represent textual data and are typically stored in variables for reuse.

  2. 2

    Single quotes and double quotes both work as string delimiters, but internal quote characters must be handled via escaping or by switching the outer delimiter type.

  3. 3

    Triple quotes enable multi-line strings without manual newline handling.

  4. 4

    Strings behave like character sequences: len() measures length, indexing is zero-based, and out-of-range indexing raises IndexError.

  5. 5

    Slicing with start:stop extracts ranges where start is inclusive and stop is exclusive; omitting start or stop uses the beginning or end of the string.

  6. 6

    Common string methods (.lower(), .upper(), .count(), .find(), .replace()) provide transformations and searches, and many return new strings rather than modifying the original.

  7. 7

    String building can be done with + concatenation, .format() placeholders, or f strings (Python 3.6+), and dir()/help() help learners discover and understand available methods.

Highlights

A string containing an apostrophe breaks if the same quote type is used to delimit it; escaping or switching to the other quote type fixes it.
Slicing uses an inclusive start index and an exclusive stop index, which is why message[0:5] yields "hello".
.replace() doesn’t modify the original string in place; the returned string must be assigned to a variable.
f strings allow direct variable insertion and even inline transformations like {name.upper()} within the placeholder.
dir() and help() provide an offline way to list string methods and read what each one does and which arguments it expects.

Topics

  • Strings
  • Quoting Rules
  • Indexing and Slicing
  • String Methods
  • String Formatting

Mentioned