Python Strings Data Type
A String is a sequence of characters used to store text. In Python, anything inside quotes is a string. It can contain letters, numbers, symbols, and whitespace.
1. Creating Strings
You can define strings in three ways:
- Single Quotes: ‘Hello’
- Double Quotes: “Hello” (Standard)
- Triple Quotes: “””…””” or ”’…”’ (For multi-line text)
name = "Python"
sentence = 'I love coding'
# Multi-line String
paragraph = """This is a string
that spans across
multiple lines."""
You can use quotes inside quotes!
- “It’s a sunny day” (Single inside double)
- ‘He said “Hello”‘ (Double inside single)
2. Characteristics of Strings
- Immutable: You cannot change a character inside a string once it is created.
- Incorrect: s[0] = “A” (This causes an Error!)
- Correct: s = “A” + s[1:] (You must create a new string).
- Ordered (Indexed): Every character has a specific numbered position.
- Iterable: You can loop through every character in a string.
3. Indexing & Slicing
Because strings are ordered sequences, we can access specific parts using square brackets [].
Visualizing Indexes
Python has two types of indexes:
- Positive Indexing: Starts from 0 (Left to Right).
- Negative Indexing: Starts from -1 (Right to Left).
Let’s look at the string “PYTHON”.
| Character | P | Y | T | H | O | N |
|---|---|---|---|---|---|---|
| Index (Pos) | 0 | 1 | 2 | 3 | 4 | 5 |
| Index (Neg) | -6 | -5 | -4 | -3 | -2 | -1 |
text = "PYTHON"
print(text[0]) # Output: P
print(text[-1]) # Output: N (The last character)
Slicing (Getting a Substring)
To extract a chunk of text, use the syntax: [start : stop : step]
- Rule: The stop index is excluded (it goes up to, but does not include, that index).
text = "somename"
# Indices: 01234567
print(text[1:5]) # "omen" (Index 1 to 4)
print(text[:4]) # "some" (Start to 3)
print(text[4:]) # "name" (4 to End)
print(text[::2]) # "smqm" (Every 2nd character)
print(text[::-1]) # "emansemos" (Reverses the string!)
4. String Operations
Concatenation (Joining)
Joining strings together using the + operator.
first = "Data"
last = "Science"
full = first + " " + last # Result: "Data Science"
Membership Check (in)
Check if a substring exists inside a string. Returns True or False.
quote = "Python is amazing"
print("amazing" in quote) # True
print("Java" not in quote) # True
5. String Methods (The Toolkit)
Python provides powerful built-in methods to manipulate text.
Note: These methods return a new string; they do not change the original.
Case Conversion
| Method | Description | Example |
|---|---|---|
| .upper() | Converts to UPPERCASE | “hi”.upper() → “HI” |
| .lower() | Converts to lowercase | “HI”.lower() → “hi” |
| .title() | Capitalizes every word | “hello world”.title() → “Hello World” |
| .capitalize() | Capitalizes first letter only | “hello world”.capitalize() → “Hello world” |
Cleaning & Replacing
| Method | Description | Example |
|---|---|---|
| .strip() | Removes whitespace (start/end) | ” hi “.strip() → “hi” |
| .replace() | Replaces old text with new | “Hi Tom”.replace(“Tom”, “Sam”) |
| .split() | Splits string into a List | “a,b”.split(“,”) → [‘a’, ‘b’] |
| .join() | Joins a List into a string | “-“.join([‘a’,’b’]) → “a-b” |
Searching
| Method | Description | Example |
|---|---|---|
| .find() | Returns index of first match | “hello”.find(“e”) → 1 |
| .count() | Counts occurrences | “banana”.count(“a”) → 3 |
| .startswith() | Checks start | “Py”.startswith(“P”) → True |
| .endswith() | Checks end | “file.py”.endswith(“.py”) → True |
Validation (Is it…?)
Used to check user input.
| Method | Checks if string contains… |
|---|---|
| .isdigit() | Only numbers (0-9). |
| .isalpha() | Only letters (a-z). |
| .isalnum() | Numbers OR letters (no symbols). |
| .isspace() | Only whitespace (spaces, tabs). |
6. String Formatting (f-Strings)
You cannot add numbers to strings directly. The modern way to solve this is f-Strings.
- Syntax: Add f before quotes and put variables in {}.
name = "Alice"
age = 25
# The Modern Way (Recommended)
print(f"My name is {name} and I am {age} years old.")
# The Old Way (Legacy - You might see this in old code)
print("My name is %s" % name) # C-style
print("My name is {}".format(name)) # .format() method
7. Escape Characters
Special characters used to insert “illegal” characters into a string.
| Escape Code | Result | Usage |
|---|---|---|
| \n | New Line | Breaks text to next line |
| \t | Tab | Adds indentation space |
| \’ | Single Quote | Prints ‘ inside single quoted string |
| \” | Double Quote | Prints ” inside double quoted string |
| \\ | Backslash | Prints a single \ |
8. Raw Strings (r-strings)
Useful for File Paths and Regular Expressions where you want backslashes \ to be treated as normal text, not escape characters.
# Normal String (Problematic for paths)
path = "C:\new\folder" # Python thinks \n is new line!
# Raw String (Solution)
path = r"C:\new\folder" # The 'r' tells Python to ignore escape codes
print(path) # Output: C:\new\folder
10. ASCII & Unicode
Computers only understand numbers. Every character maps to a specific number (ASCII code).
- ord(): Character to Number.
- chr(): Number to Character.
print(ord("A")) # 65
print(chr(97)) # 'a'