Python String Data Type
1. What is a String in Python?
A string is defined as a sequence of characters. These characters can include:
- Alphabets (A–Z, a–z)
- Digits (0–9)
- Special symbols (@, #, $, etc.)
- Spaces
Example
s = "John"
Here, "John" is a string because it is a sequence of characters. In Python, the string data type is represented as:
<class 'str'>
2. How to Create Strings
Python provides multiple ways to create strings, making it very flexible compared to many other languages.
Using Single Quotes
s = 'John'
print(type(s))
Using Double Quotes
s = "Durga"
print(type(s))
In both cases, the output will be:
<class 'str'>
This means that both single quotes and double quotes are equally valid in Python, and the choice between them is mostly based on readability and convenience.
3. Which One Should You Use?
You can use either single quotes or double quotes, but:
- Single quotes are often preferred because they are simpler and cleaner
- Double quotes are useful when your string contains single quotes
Ultimately, the choice depends on your comfort and the situation.
4. No Character (char) Data Type in Python
In Python:
ch = 'a'
print(type(ch)) # <class 'str'>
This means:
- Python does not have a separate char data type
- Even a single character is treated as a string of length 1
This is a key concept that beginners must clearly understand.
5. Multi-Line Strings and Triple Quotes
In Python, we have a special feature that allows us to define strings across multiple lines using triple quotes.
s = '''Hello World
from me'''
print(s)
Output:
Hello World
from me
You can also use triple double quotes:
s = """Hello World
from me"""
print(s)
Python String Indexing
Indexing is the mechanism that allows you to access individual characters within a string. A string in Python is a sequence of characters, and since it is a sequence, each character has a specific position within that sequence. This position is called an index.
s = "James"
Here, the string consists of 5 characters:
J a m e s
Each character is associated with an index.
Positive Indexing (Left to Right)
In Python, indexing always starts from 0, which means the first character is at index 0, the second at 1, and so on.
Character: J a m e s
Index: 0 1 2 3 4
s = "James"
print(s[0]) # First character
print(s[3]) # Fourth character
This is called positive indexing, where we move from left to right.
What Happens If Index is Out of Range?
Every string has a limited number of characters. If you try to access an index that does not exist, Python will raise an error.
s = "James"
print(s[100]) # IndexError: string index out of range
Negative Indexing (Right to Left)
Now comes one of the most interesting and powerful features of Python—negative indexing. Unlike many other programming languages such as C, C++, or Java, Python allows you to access string elements from the end using negative indices.
Representation
Character: J a m e s
Index: 0 1 2 3 4
Negative: -5 -4 -3 -2 -1
- Negative indexing starts from -1
- -1 represents the last character
- Indexing proceeds backward
Positive vs Negative Indexing
Let us compare both approaches for clarity:
| Direction | Index Type | Starting Point | Example |
|---|---|---|---|
| Left → Right | Positive Index | 0 | s[0] |
| Right → Left | Negative Index | -1 | s[-1] |
Out-of-Range with Negative Index
Just like positive indexing, negative indexing also has limits.
s = "James"
print(s[-6]) # IndexError: string index out of range
Because valid negative indices are only from -1 to -5.
Python Slice Operator
Before understanding the Python concept, let us first understand the general meaning of the word slice. A slice simply means a piece or a part of something. For example, if you cut an apple into pieces, each piece is called a slice. In Python, the same idea applies:
A slice of a string means a part of that string. So, if you have a string and you want only a portion of it, you can extract it using the slice operator.
Why Do We Need Slicing?
Let us consider a simple string:
s = "abcdefghijklmnopqrstuvwxyz"
Now:
- If you want a single character → use indexing (s[3])
- But if you want multiple characters → you need slicing
For example, if you want characters from index 3 to 7, indexing alone is not sufficient. This is where slicing comes in.
Syntax of Slice Operator
The syntax for slicing in Python is:
string[begin_index : end_index]
- The slice includes begin_index
- The slice excludes end_index
So effectively, it returns characters from:
begin_index → (end_index - 1)
s = "abcdefghijklmnopqrstuvwxyz"
print(s[3:9]) # defghi
Default Values in Slicing
One of the most convenient features of Python slicing is that both indices are optional, and Python provides sensible default values.
Case 1: Missing Begin Index
print(s[:9]) # abcdefghi
- Default begin index = 0
- Returns characters from start to index 8
Case 2: Missing End Index
print(s[3:]) # defghijklmnopqrstuvwxyz
- Default end index = end of string
- Returns characters from index 3 to the end
Case 3: Missing Both
print(s[:])
Returns the entire string. This is often used to create a copy of the string.
What Happens with Out-of-Range Indices?
This is where slicing behaves differently from indexing.
print(s[3:1000]) # defghijklmnopqrstuvwxyz
Even though 1000 is out of range, Python does not throw an error.
When Begin Index is Greater Than End Index
print(s[5:1]) # ""
- Python moves from left to right by default
- Starting at index 5, it cannot reach index 1 in forward direction
- So, result is an empty string
The + Operator with Strings
Let us begin with the most commonly used operator on strings—the plus (+) operator.
s = "Hello" + "World"
print(s)
The + operator, when applied to strings, performs concatenation, which means it joins two strings together into a single string.
Unlike some languages (like Java), Python is strict about types when using the + operator.
s = "Durga" + 10 # ❌ Error
This results in:
TypeError: can only concatenate str (not "int") to str
Both operands must be strings. Mixing string with non-string (like int) is not allowed
The * Operator with Strings
Now let us look at another interesting operator—the multiplication (*) operator.
s = "James" * 3
print(s) # JamesJamesJames
- The * operator performs string repetition
- It repeats the string n number of times
s = "Durga" * "Soft" # TypeError: can't multiply sequence by non-int of type 'str'
Practical Use Case of * Operator
The repetition operator is often used for formatting output, especially when printing patterns or separators.
print("*" * 10)
print("Hello World")
print("*" * 10)
Output:
**********
Hello World
**********