Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today we're meeting strings. What do you think a string is in Python?
Text, like a name or a sentence?
Yes. A string is a sequence of characters enclosed in quotes, and the characters can be letters, digits, spaces or symbols. So is '2024' a string?
It has quotes around it, so yes, even though it looks like a number.
Exactly. The quotes decide the type: type('2024') shows <class 'str'>, while type(2024) shows <class 'int'>.
Do spaces count as characters too?
They do, and so does punctuation. 'Hi Sam!' is seven characters long: H, i, the space, S, a, m and the exclamation mark.
Python accepts single quotes, double quotes and triple quotes. Does the choice change the string?
I'd guess not, because 'Hello' and "Hello" look like the same word.
Right, they are equal. The choice only matters when the text itself contains a quote mark. How would you store It's raining?
With double quotes, because an apostrophe would end a single-quoted string too early.
Good. And for text that contains double quotation marks, such as a sign that says "Stop", put single quotes around it.
What if the text has both kinds of quote, or runs over several lines?
Then use triple quotes. Inside them, both quote marks are ordinary characters, and the line breaks you type are kept.
One more idea for today. Why do we call a string a sequence?
Because its characters come in a fixed order?
Exactly. Every character has a position, and Python counts positions from 0, so in 'Python' the P is at position 0.
Is that how we'll pick out single letters later?
Yes, that's indexing, which comes next. After that we'll join strings, repeat them and use methods such as upper() to change them.
A sequence of characters enclosed in quotes; its type is str.
Single, double and triple quotes all create the same type of value, so 'Hi' and "Hi" are equal.
Use quotes that do not appear in the text, such as double quotes around an apostrophe and single quotes around double quotation marks.
Triple quotes let a string span several lines and keep its line breaks.
Digits inside quotes are characters, so "25" stays a str until int() converts it.
Every character, including spaces and punctuation, has a position, which later lessons use for indexing and slicing.
The three examples from this lesson, printed together with their types:
name = "Alice"
greeting = 'Hello'
multiline = """This is
a multi-line string"""
print(name)
print(greeting)
print(multiline)
print(type(name), type(greeting), type(multiline))Choose quotes that do not clash with the text:
reply = "I'm on my way"
sign = 'The board says "Closed today"'
story = """Ravi said "I'm ready" and stood up."""
print(reply)
print(sign)
print(story)Spaces and punctuation are characters, and digits in quotes are text until you convert them:
message = "Hello, world!"
print(len(message))
year_text = "2024"
print(type(year_text))
print(int(year_text) + 1)
empty = ""
print(len(empty))Wrap the words in quotes and text is what you get; if the text holds that quote, pick another set.
Maya typed the message It's my birthday inside single quotes, and Python complained. Her friend explained that the apostrophe had closed the string too early, so Maya switched to double quotes. Later she wrote her whole party invitation over several lines inside triple quotes.
Clash, then switch - if the quote mark you used also appears in the text, switch to the other kind, and go triple when you need both kinds or several lines.
S.D.T. - Single quotes for simple text, Double quotes around an apostrophe, Triple quotes for both kinds of quote or several lines.
Term
What is a string in Python?
Click to flip
Term
Which three kinds of quotes can create a string?
Click to flip
Term
Which quotes suit text that contains an apostrophe, such as It's late?
Click to flip
Term
What do triple quotes allow?
Click to flip
Term
Is "99" a number?
Click to flip
Term
Do spaces and punctuation count as characters?
Click to flip
A sequence of characters enclosed in quotes, used to represent text; its type is str.
A single letter, digit, space, punctuation mark or symbol inside a string.
An ordered collection in which every item has a position; a string is a sequence of characters.
The quote marks that show where a string begins and ends; they are not part of the string's value.
A string written between three single or three double quotes, which can span several lines and keeps its line breaks.
A string with no characters at all, written as "" or ''.
Python's name for the string type; type("Hi") prints as <class 'str'>.
A string in Python is a sequence of characters enclosed in quotes, capable of being used in various operations.
This section introduces strings as sequences of characters in Python, highlighting how they can be declared, indexed, and manipulated using built-in methods. It sets the foundation for understanding more complex string operations and formatting.
A string is fundamentally a sequence of characters in Python. Strings can be created using single quotes (' '), double quotes (" ") or triple quotes (''' ''' or """ """). All three create the same type of value, str, so 'Hello' and "Hello" are exactly the same string. Triple quotes can also run over several lines.
A string can hold any characters: letters, digits, spaces, punctuation marks and symbols. Every one of them counts, so "Hello, world!" is 13 characters long, including the comma, the space and the exclamation mark. A string can even be empty: "" contains no characters at all.
Anything inside quotes is text, even when it looks like something else. "2024" is a str, while 2024 is an int; "True" is a str, while True is a bool. To calculate with digits stored as text, convert them first: int("2024") + 1 gives 2025, but "2024" + 1 raises a TypeError.
Python ends a string at the first quote mark that matches the one that opened it, so choose quotes that do not appear in the text:
"It's raining".'The sign said "Stop"'.Writing 'It's raining' fails, because the apostrophe closes the string straight after It, and Python cannot make sense of the rest of the line.
A string in single or double quotes must fit on one line. Triple quotes let a string span several lines, and the line breaks are kept exactly as you typed them:
menu = """Today's specials:
Masala dosa
Mango lassi"""
print(menu)
print(type(menu))The apostrophe in Today's causes no trouble here, because inside triple quotes single and double quote marks are ordinary characters.
Because a string is a sequence, its characters stay in a fixed order and each one has a position, counted from 0. The next lessons build on this: indexing and slicing pick out characters by position, operations join and repeat strings, and built-in methods such as upper() transform them.