Assigning Strings
In Python, you can assign a string to a variable by enclosing the string within either single quotes ('') or double quotes ("").
Syntax:
Python
variable_name =
"string value"
Example:
Python
greeting =
"Hello, world!"
name =
'Alice'
Key points:
· You can use either single or double quotes to define a string.
· The variable name on the left side of the equal sign stores the string value.
· Python is dynamically typed, so you don't need to declare the variable's type beforehand.
Example with output:
Python
message =
"Welcome to Python!"
print(message)
This will output:
Welcome to Python!
Multiline Strings
Python offers a convenient way to handle strings that
span multiple lines using triple quotes (either single or double).
Syntax:
Python
multiline_string =
"""This is a multiline string.
It can span multiple lines.
You can include newlines, tabs, and other characters."""
Explanation:
· The opening and closing triple quotes define the boundaries of the multiline string.
· All characters within the triple quotes, including whitespace and newlines, are preserved as part of the string.
Example:
Python
poem = """
Twinkle, twinkle, little star,
How I wonder what you are.
Up above the world so high,
Like a diamond in the sky.
"""
print(poem)
Key points:
· Triple quotes provide a clean and readable way to define multiline strings.
· They are often used for docstrings to document functions, classes, and modules.
· The indentation within the triple quotes is preserved in the string.
Quotes Inside Quotes
Python offers several ways to handle quotes within strings:
Method 1: Alternating Quotes
The simplest way is to use different types of quotes for the outer and inner strings:
Python
text =
"He said, 'Hello there!'"
print(text)
Method 2: Escape Sequences
You can use the backslash (\
)
to escape quotes within a string:
Python
text =
"He said, \"Hello there!\""
print(text)
Method 3: Triple Quotes
For multi-line strings or strings with both single and double quotes, triple quotes are useful:
Python
text =
"""He said, "Hello there!" and she replied, 'Nice to meet you.'"""
print(text)
Example
Python
# Combining methods
text =
"She said, 'He said, \"Hello there!\"\'"
print(text)
Key points:
· Choose the method that best suits your string content and readability.
· Be consistent in your use of quotes to avoid syntax errors.
· For complex strings with nested quotes, consider using triple quotes for better clarity.
Escape Characters
Escape characters are special characters preceded by a backslash (\
) that represent certain characters or actions. They
are used to insert characters that are difficult or impossible to type
directly, or to represent special characters that have a specific meaning
within a string.
Here are some common escape sequences in Python:
Escape Sequence |
Description |
|
Newline |
|
Tab |
|
Backslash |
|
Single quote |
|
Double quote |
|
Carriage return |
|
Backspace |
|
Form feed |
|
Octal character (up to three digits) |
|
Hexadecimal character (up to two digits) |
Python
text =
"This is a \nmulti-line string."
print(text)
text2 =
"The character \\ is called a backslash."
print(text2)
text3 =
"He said, \"Hello there!\""
print(text3)
Raw Strings
To prevent the interpretation of escape sequences, you
can use a raw string by prefixing the string with r
or R
:
Python
raw_text =
r"This is a raw string\n without newlines"
print(raw_text)
Note: Raw strings are often used for regular expressions and file paths where backslashes have special meaning.
By understanding and using escape characters effectively, you can create complex and accurate strings in your Python code.