9.1 String Declarations
In Python, strings are sequences of characters used for storing and representing text information. There are several ways to create strings, giving you flexibility in declaring and manipulating them (Strings are immutable, as you already know).
Main ways to declare strings
- Single Quotes:
string
= 'Hello, buddy!' - Double Quotes:
string
= "Hello, buddy!"
Both ways are equivalent, and the choice depends on the programmer's preference and which quotes are used within the string itself (to avoid escaping them).
Multiline Strings
To declare multiline strings, use triple quotes. This can be triple single quotes (''')
or triple double quotes (""")
. This method is convenient for text spanning multiple lines:
multiline_string = """First line
second line
third line"""
Escaping Characters
In Python, characters in strings can be escaped using a backslash (\
). This allows you to insert special characters like a newline (\n
), a tab (\t
), or the backslash itself (\\
).
escaped_string = "He said: \"Hello, buddy!\""
We'll dive deeper into escaping in a couple of lectures ahead.
Raw Strings
Raw strings (raw string)
are strings where escaping special characters is disabled. They are declared with the r
prefix:
raw_string = r"In this string \n is not interpreted as a newline."
String Formatting
Python offers several methods for formatting strings. The most powerful is f-strings
, available from Python 3.6 onwards. They let you embed Python expressions directly into a string:
name = "World"
formatted_string = f"Hello, {name}!"
String Concatenation
Strings can be created by combining several strings using the +
operator or the join()
method for more complex scenarios:
greeting = "Hello, " + "buddy!"
String Immutability
Remember, strings in Python areimmutable. This means that any operations modifying a string will create a new string.
9.2 Nested Quotes
In Python, working with strings containing nested quotes requires close attention to syntax details to ensure code correctness and avoid syntax errors.
You can't just write a quote inside text that's enclosed in quotes. For example, when you write code like:
escaped_string = "He said: "Hello, world!"… "
Here's what Python sees:
escaped_string = "He said: "Hello, world!"… "
It looks for the nearest closing quote, not the last one in the string, so to add a quote inside text, you need to put in a bit of extra effort. Below is an overview of main techniques and methods for managing nested quotes in Python strings:
Using Different Types of Quotes
Python lets you use single (')
and double (")
quotes to define strings. This allows you to nest one type of quote within another:
quote = "He said, 'Hello, world!'"
# or
quote = 'He said, "Hello, world!"'
Escaping Quotes
When you need to use the same quotes within a string, you can use a backslash (\)
to escape the quotes:
quote = "He said,\"Hello, world!\" "
Multiline Strings
For creating multiline strings containing nested quotes, it's convenient to use triple quotes (''' or """)
:
multiline_quote = """This is a long quote that contains multiple types of quotation marks:
"double quotes" and 'single quotes'. It spans multiple lines."""
GO TO FULL VERSION