Python online course

Python Strings: Creating Text and Using Quotes

Learn how Python strings represent text, how to create them with single, double, and triple quotes, and how to use escapes for quotes, newlines, tabs, and backslashes.

A string is Python's data type for textual character data. It represents an ordered sequence of characters. A character can be a letter, digit, space, punctuation mark, or special character such as a newline.

Strings are used for names, messages, labels, descriptions, file paths, and text entered by a user. For example, "42" is a string containing two digit characters, while 42 is a numeric value. They may look similar when displayed, but Python treats them as different types of values.

Before working with strings, it helps to know how to run Python code, use the interactive prompt, and assign values to variables.

Creating and displaying a string

A string literal is a string value written directly in source code and enclosed by quotation marks. A variable assignment stores that value under a name:

greeting = "Hello, Python!"

Use the print() function to display the value:

greeting = "Hello, Python!"
print(greeting)

Output:

Hello, Python!

The quotation marks delimit the literal in the source code. They are not normally part of the value printed by print().

Single-quoted strings

One ordinary way to create a one-line string is to place one single quotation mark, also called an apostrophe, at each end:

name = 'Mina'
message = 'Welcome to the course.'

Single quotes are convenient for ordinary one-line text. The opening and closing quote characters are the string's delimiter: they mark where the literal begins and ends.

Double-quoted strings

Python also supports double quotation marks:

name = "Mina"
message = "Welcome to the course."

Single-quoted and double-quoted literals generally create the same string type and the same value:

first = 'Python'
second = "Python"

print(first == second)
True

The choice between single and double quotes is mainly useful when the text itself contains quotation characters. Choose the delimiter that lets the content remain easy to read.

Choosing a string delimiter

Single quotation marks — Best for ordinary one-line text; cannot span physical source lines directly; leaves double quotation marks unescaped.

Double quotation marks — Best for ordinary one-line text; cannot span physical source lines directly; leaves apostrophes unescaped.

Triple single quotation marks — Useful for multiline text; can span multiple lines; can often contain ordinary single or double quote characters.

Triple double quotation marks — Useful for multiline text; can span multiple lines; can often contain ordinary single or double quote characters.

Putting quotes inside string content

A delimiter conflict happens when the same quote character is used both to end a string and as text inside it. For example, this code is invalid because the apostrophe in Sam's looks like the ending delimiter:

# SyntaxError
# sentence = 'Sam's jacket is blue.'

A simple solution is to use double quotes around text containing an apostrophe:

sentence = "Sam's jacket is blue."
print(sentence)

Similarly, use single quotes around text containing double quotation marks:

quote = 'The sign says "Open".'
print(quote)

Another solution is to escape the matching quote. An escape sequence is a backslash-based notation that tells Python to treat a character specially:

quote = "She said, \"Start now!\""
print(quote)
She said, "Start now!"

Triple-quoted strings and line breaks

A triple quote is three matching quotation marks. Python supports both triple single quotes and triple double quotes:

notice = '''First line
Second line
Third line'''

print(notice)

Output:

First line
Second line
Third line

Triple-quoted literals can contain line breaks directly, so they are useful for notices, paragraphs, and other multiline strings. They can also contain ordinary quote characters without ending the string in many cases:

description = '''The label says "Ready".
It belongs to Alex's device.'''
print(description)

Whitespace inside a triple-quoted literal is part of the resulting value. That includes indentation and any leading or trailing newline:

notice = """
    Important message
"""

This value begins with a newline, and the text line includes spaces before Important. Align multiline literals carefully, or construct the text in a way that does not include unwanted whitespace.

Escape characters and escape sequences

The backslash character, written as \, is Python's common escape character. It introduces an escape sequence, which is source-code notation for a special character or for a quote or backslash that should appear as text.

\' — An apostrophe — Put an apostrophe inside a single-quoted literal.

\" — A double quotation mark — Put a double quote inside a double-quoted literal.

\\ — One literal backslash — Display a backslash character.

\n — A newline character — Move following output to the next line.

\t — A tab character — Add horizontal spacing between text.

Escape notation belongs to the source code. The resulting character is what Python stores in the string and what print() renders. For example, \n occupies two visible positions in source code, but represents one newline character in the value.

Escaping quotes

single_quote = 'It\'s ready.'
double_quote = "Use \"quotes\" here."

print(single_quote)
print(double_quote)
It's ready.
Use "quotes" here.

Newlines and tabs

Escape sequences are useful when the complete string must remain on one source line:

details = "Name:\tMina\nStatus:\tReady"
print(details)
Name:   Mina
Status: Ready

The exact visual width of a tab can depend on the console or editor, but \t always represents a tab character.

Literal backslashes

Because a backslash begins an escape sequence, write two backslashes in source code when the resulting string should contain one literal backslash:

path = "C:\\Users\\Mina\\notes.txt"
print(path)
C:\Users\Mina\notes.txt

Raw strings are another topic that can help with backslash-heavy text, but doubling backslashes is the fundamental technique shown here.

What print() displays

print() displays the characters represented by a string. It renders newlines and tabs rather than showing their backslash notation:

text = "one\ntwo\tthree"
print(text)
one
two    three

To inspect a representation in which special characters remain visible as escape notation, use repr():

print(repr(text))
'one\ntwo\tthree'

The first output is formatted for a reader. The second is a representation that makes the newline and tab notation visible. They describe the same underlying string value.

Common string mistakes

An apostrophe ends a single-quoted string

Problem: Python reports a SyntaxError for code such as 'Jordan's book'.

Cause: The apostrophe is interpreted as the closing delimiter.

Fix: Use "Jordan's book", or escape the apostrophe as 'Jordan\'s book'. See syntax and logical errors for more about syntax problems.

Double quotation marks end a double-quoted string

Problem: A string such as "She said "Go"" is not parsed as intended.

Cause: The inner double quote closes the literal early.

Fix: Use single quotes outside, or escape the inner quotes as "She said \"Go\"".

Expected a new line, but output stays on one line

Cause: The string contains no newline character.

Fix: Include \n, or use a triple-quoted literal with an actual line break.

Unexpected spaces or blank lines in multiline output

Cause: Whitespace written inside a triple-quoted literal becomes part of the string.

Fix: Check for leading indentation, a newline immediately after the opening delimiter, and extra newlines before the closing delimiter.

Key points

  • A string is an ordered sequence of characters used to represent text.
  • A string literal is text written directly in Python code with a delimiter.
  • Single and double quotes generally produce equivalent string values.
  • Choose the opposite quote style when the text contains an apostrophe or quotation mark.
  • Escape a matching quote with a backslash when changing delimiters is not convenient.
  • Triple quotes allow line breaks directly, but preserve whitespace inside the literal.
  • \n, \t, and \\ represent a newline, tab, and literal backslash.
  • print() renders special characters, while repr() can show their escape notation.

After learning to create strings, continue with accessing individual characters, concatenating strings, and string functions. For user-entered text, see getting user input.