Nearby lessons
19 of 108Python - String Data Type
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
main.py
Python Programming
Single Quotes and Double Quotes
Single quotes and double quotes are used to create single-line strings.
Both produce the same result.
main.py
Python Python
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: """ """
main.py
durga software solutions durga software solutions
Triple Single Quotes
main.py
Python Programming
Triple Double Quotes
main.py
Python Programming
Using Quotes Inside a String
Triple quotes allow you to use both single quotes and double quotes inside a string without escaping them.
main.py
This is "Python" class
Another Example
main.py
This is 'Python' class
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.
main.py
The "Python Notes" by 'durga' is very helpful The "Python Notes" by 'durga' is very helpful
Escape Characters in Strings
You can also use the backslash to include quote symbols inside a string.
main.py
The "Python Notes" by 'durga' is very helpful
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
main.py
P y n
Index Out of Range
Accessing an index outside the string length raises an error.
main.py
IndexError: string index out of range
String Slicing
Slicing returns a part of a string.
Syntax:
string[start:end]
Examples of String Slicing
main.py
ytho thon Pyth Python
String Multiplication
The * operator repeats a string multiple times.
main.py
PythonPythonPython
len() Function
The len() function returns the number of characters in a string.
main.py
6
Strings are Immutable
Strings are immutable, which means their characters cannot be changed after creation.
main.py
TypeError
Character Data Type
Python does not have a separate char data type.
A single character is also stored as a string.
main.py
No output captured.
Common String Operations
main.py
P n yth PythonPython 6
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 |