Nearby lessons
19 of 159Python - String Data Type
- Define the string data type and how it stores text
- Create strings using single, double, and triple quotes
- Access characters using positive and negative indexing
- Extract parts of a string using slicing
- Use len() and the * operator with strings
- Explain why strings are immutable
Introduction
The str data type is used to represent text (strings).
A string is a sequence of characters enclosed in quotes.
You can create strings using:
- Single quotes (
' ') - Double quotes (
" ")
Example
Single Quotes and Double Quotes
Single quotes and double quotes are used to create single-line strings.
Both produce the same result.
Multi-line Strings
Single quotes and double quotes cannot be used to create multi-line strings.
To create multi-line strings, use triple quotes.
How to Define Multi-line String Literals
We can define multi-line string literals by using triple single quotes or triple double quotes.
Triple Single Quotes: ''' '''
Triple Double Quotes: """ """
Triple Single Quotes
Triple Double Quotes
Using Quotes Inside a String
Triple quotes allow you to use both single quotes and double quotes inside a string without escaping them.
Another Example
Using Both Single and Double Quotes
Triple quotes are helpful when a string contains both single quotes and double quotes.
Without triple quotes, the string can become invalid and raise a syntax error.
Example of an invalid string that mixes both quote symbols without escape characters:
s = 'The "Python Notes" by 'durga' is very helpful'
# Result: SyntaxErrorEscape Characters in Strings
You can also use the backslash to include quote symbols inside a string.
String Indexing
Python strings support indexing.
Indexing starts from 0.
| Character | P | y | t | h | o | n |
|---|---|---|---|---|---|---|
| Positive Index | 0 | 1 | 2 | 3 | 4 | 5 |
| Negative Index | -6 | -5 | -4 | -3 | -2 | -1 |
Accessing Characters
Index Out of Range
Accessing an index outside the string length raises an error.
String Slicing
Slicing returns a part of a string.
Syntax:
string[start:end]
Examples of String Slicing
String Multiplication
The * operator repeats a string multiple times.
len() Function
The len() function returns the number of characters in a string.
Strings are Immutable
Strings are immutable, which means their characters cannot be changed after creation.
Character Data Type
Python does not have a separate char data type.
A single character is also stored as a string.
Common String Operations
Applications of Strings
Strings are widely used to store and process text data.
- User names
- Messages
- Email addresses
- File names
- Web content
Important Points
- The
strdata type stores text. - Strings can be created using single, double, or triple quotes.
- Strings support indexing and slicing.
- Python does not have a separate
chardata type. - Strings are immutable.
Key Points
- The
strdata type represents text. - Strings are sequences of characters.
- Triple quotes are used for multi-line strings.
- Triple quotes are also useful when both quote symbols appear in the same string.
- Use indexing to access characters.
- Use slicing to extract part of a string.
- Use
len()to find the string length.
Quick Summary
| Feature | Description |
|---|---|
| Data Type | str |
| Purpose | Stores text data |
| Quotes | Single, Double, Triple |
| Indexing | Supported |
| Slicing | Supported |
| Mutable | No (Immutable) |
| Character Type | Represented using str |
- The str data type represents text as a sequence of characters
- Triple quotes are used to create multi-line strings
- Indexing starts from 0, and negative indexes access from the end
- The * operator repeats a string, and len() returns its length
- Python strings are immutable, and there is no separate char type