About Lesson
Introduction to Strings in Python
A string in Python is a sequence of characters. It is one of the most commonly used data types and is used to represent text-based data.
- Strings are immutable in Python, meaning once a string is created, it cannot be modified.
- Strings are enclosed in either single quotes (‘ ‘), double quotes (” “), or triple quotes (”’ ”’ or “”” “””) for multi-line strings.
Example:
Python
# Single-line strings / single quote string / double quote string
str1 = 'Hello, World!'
str2 = "Hello, World!"
# Multi-line strings / multiline strings
str3 = """This is
a multi-line
string."""
In Python, an escape character (or escape sequence) is a backslash (\
) followed by a character that has a special meaning. It is used within strings to represent characters that would otherwise be difficult to include directly or to signal that the character following the backslash should be treated in a special way.