Module 7 · Strings — topic 1 of 22
Creating strings
All the ways to write string values in Python: single, double and triple quotes, raw strings, converting with str(), building from other strings, and the empty string.
Learning objectives
- Write string literals with each quote style
- Create strings from other values with str() and join()
- Use raw strings for backslash-heavy text
Definition
All the ways to write string values in Python: single, double and triple quotes, raw strings, converting with str(), building from other strings, and the empty string.
Introduction
A string is an ordered sequence of characters. You have used strings since your first print(); this module looks at them properly, starting with every way to create one.
Python treats text as first-class data: strings can be stored, compared, searched, sliced and transformed with dozens of built-in methods.
Why it matters
Text is everywhere: names, addresses, messages, file contents, web pages and data from APIs. Choosing the right way to write a string avoids escaping headaches, especially with quotes, apostrophes and Windows file paths.
Detailed explanation
Single quotes 'text' and double quotes "text" create identical strings. Pick the one that doesn’t appear inside the text: "It's ready" or 'He said "hi"'.
Triple quotes """...""" or '''...''' allow line breaks inside the string and both kinds of quotes without escaping.
A raw string has an r prefix: r"C:\new\test". Backslashes are kept literally instead of starting escape sequences, which is useful for file paths and regular expressions.
str(value) converts any value to its string form: str(42) is "42", str([1, 2]) is "[1, 2]".
Adjacent string literals are joined automatically: "Hello, " "world" becomes "Hello, world". This helps split a long literal across lines inside parentheses.
The empty string "" has no characters, length 0, and is falsy.
Strings can contain any Unicode character, including emoji and non-Latin scripts.
Syntax
- Single and double quotes are interchangeable.
- Triple quotes span lines.
- The r prefix keeps backslashes literally.
- str() converts other values.
Code examples
Quote styles
single It's simple She said "go" Line one Line two
Raw strings and str()
C:\new\data 420.5 0 False
More examples
Building a label from data
Item: Notebook | Price: 45.0 | Available: True str 46
Step-by-step explanation
- The parentheses let the expression continue over several lines.
- str() converts the float and the Boolean so they can be joined with +.
- " | " and "Price: " on the next line are adjacent literals, joined automatically.
- The result is a single 46-character str.
Expected output
Running “Building a label from data” prints exactly:
Practical applications
- Configuration files contain Windows paths that are easiest to write as raw strings.
- Email templates are often stored in triple-quoted strings.
- APIs return JSON text that is converted to and from strings.
Best practices
- Choose the quote style that avoids escaping.
- Use raw strings for paths and regular expressions.
- Be consistent: most projects use double quotes by default.
Common mistakes
A backslash path without r
Avoid
Prefer
\n and \t are escape sequences, so the normal string contains a newline and a tab instead of the folder names.
Unescaped matching quote
Avoid
Prefer
The apostrophe ends the single-quoted string early, causing a SyntaxError.
Key takeaways
- Strings can be written with single, double or triple quotes.
- Raw strings keep backslashes literally.
- str() converts any value to a string.
Try it yourself
- 1Print the sentence: It's called "Python" using a single string literal.
- 2Store a three-line poem in a triple-quoted string and print its length.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
17 questions cover the “Strings” module. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is printed?