Guide Python Beginner

9.2 Creating Strings

Single, double, and triple quotes, multiline and empty strings, raw strings for paths and regex, and why every Python 3 str is Unicode by default.

3 min read

Single and Double Quotes

Single and double quotes are functionally identical — the choice is purely stylistic, except when the text itself contains a quote character, in which case using the other kind avoids escaping (see 9.3 Escape Characters).

>>> s = 'Hello'
>>> s = "It's DevOps"      # double quotes avoid escaping the apostrophe
"It's DevOps"

Triple Quotes and Multiline Strings

Triple single or double quotes ('''...''' or """...""") let a string span multiple lines and contain unescaped quote characters. They’re also how Python docstrings are written, and are the standard way to embed SQL queries, HTML/email templates, or usage text directly in code.

>>> query = """
... SELECT * FROM users
... WHERE active = true
... """
>>> print(query)
SELECT * FROM users
WHERE active = true

Empty Strings

An empty string ("") is a valid, zero-length str — often used as an accumulator’s starting value or a default “no value” placeholder. It’s falsy in a boolean context, a common way to check for missing text.

>>> s = ""
>>> len(s)
0
>>> bool(s)      # empty string is falsy
False

Raw Strings (r"...")

A string literal prefixed with r treats backslashes as literal characters instead of escape-sequence introducers — essential for Windows file paths and regex patterns (see 9.10 Regular Expressions with Strings), both of which use backslashes heavily.

>>> path = r"C:\new\test"
>>> path
'C:\\new\\test'

Unicode Strings

Every Python 3 str is Unicode by default — no special prefix needed, unlike Python 2’s u"..." — so accented letters, symbols, and non-Latin scripts just work out of the box.

>>> s = "héllo wörld ünïcödé"
>>> print(s)
héllo wörld ünïcödé

Quick Interview Answer

“Single and double quotes are interchangeable; pick whichever avoids escaping a quote character inside the text. Triple quotes span multiple lines and are also how docstrings are written. A raw string literal (r"...") turns off backslash escaping, which is essential for Windows paths and regex patterns that would otherwise need every backslash doubled. Every Python 3 str is Unicode natively — there’s no separate byte-string-by-default mode like Python 2 had.”

Common Mistakes

  • Forgetting a raw string’s backslash still can’t end the literal with an odd number of trailing backslashes (r"path\" is a syntax error) — Python’s raw-string handling isn’t quite “no escaping at all.”
  • Using a regular string for a Windows path or regex pattern and getting mangled output from unintended escape sequences like \n or \t appearing mid-path.
  • Assuming triple-quoted strings are a distinct type from single/double-quoted ones — they all produce the exact same str object; triple quotes only affect what characters can appear unescaped.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form