[Follow here] for daily coding insights.
DSA SERIES – Chapter 3: STRINGS (Python Version)
1. What is a String in Python?
In Python:
-
String = Immutable sequence of Unicode characters
-
Internally stored as an array of characters
-
Since strings are immutable, any modification creates a new string in memory
Example:
s = "Hello"
2. Memory Representation (Very Important)
Python strings use:
-
Unicode encoding
-
Immutable memory blocks
-
String interning (optimization where Python reuses identical string objects)
Example:
a = "hello"
b = "hello"
print(a is b) # True (interned)
3. String Declaration
s1 = "Python"
s2 = 'DSA'
s3 = """Multi-line
string"""
Python allows:
✔ Single quotes
✔ Double quotes
✔ Triple quotes (multi-line)
4. Accessing Characters (Indexing)
Python supports:
-
Positive indices (0 → n-1)
-
Negative indices (-1 → last element)
s = "HELLO"
print(s[0]) # H
print(s[-1]) # O
5. Slicing (Very Common in DSA)
s = "ABCDEFG"
print(s[1:5]) # BCDE
print(s[:4]) # ABCD
print(s[3:]) # DEFG
print(s[::-1]) # reverse
6. Strings Are Immutable
You cannot modify a character:
s = "hello"
s[0] = 'Y' # ❌ Error
To “modify” → create a new string:
s = "hello"
s = 'Y' + s[1:]
print(s) # Yello
7. String Operations
✔ len()
len("Python") # 6
✔ Concatenation
"Hello" + "World"
✔ Repetition
"Hi" * 3 # HiHiHi
✔ Membership
print("Py" in "Python") # True
8. Useful Built-In String Methods
Changing Case
s.lower()
s.upper()
s.title()
s.capitalize()
Checking Content
s.isalpha()
s.isdigit()
s.isalnum()
s.isupper()
s.islower()
s.isnumeric()
Searching
s.find("on") # returns index or -1
s.rfind("on")
s.startswith("Py")
s.endswith("on")
Replace
s.replace("a", "AA")
Splitting & Joining
s.split()
" ".join(["A","B","C"])
9. Iterating Through a String
for ch in "Hello":
print(ch)
10. Converting Between Strings and Other Types
int("123")
str(1010)
list("Python")
11. Time Complexities (Python String Ops)
| Operation | Time |
|---|---|
| Access char | O(1) |
| Search substring | O(n) |
| Concatenation | O(n) |
| Slicing | O(k) (k = sliced length) |
| Reversing | O(n) |
| Split | O(n) |
| Replace | O(n) |
Reason: Due to immutability, new strings must be created.
12. Common DSA-Level String Problems
✔ Reverse a string
s[::-1]
✔ Check palindrome
s == s[::-1]
✔ Count vowels
sum(1 for c in s if c.lower() in 'aeiou')
✔ Frequency count
from collections import Counter
Counter(s)
✔ Remove duplicates (maintain order)
result = ""
for ch in s:
if ch not in result:
result += ch
13. 20 Practice Questions (Python Edition)
Questions
-
What is a string in Python?
-
Are strings mutable or immutable?
-
How does slicing work?
-
How to reverse a string in one line?
-
Difference between find() and index().
-
Write code to count vowels.
-
How to check if a string is palindrome?
-
Explain negative indexing.
-
Write code to remove duplicates.
-
What is string interning?
-
Why is concatenation O(n)?
-
How to split a string into words?
-
How to join a list into a string?
-
Write code to check if two strings are anagrams.
-
Write code to count words.
-
Difference between isdigit() and isnumeric().
-
Write code to find frequency of characters.
-
Explain difference between
==andis. -
Why slicing does not modify original string?
-
Write code to convert string to list of characters.
Answers
-
Immutable sequence of Unicode characters.
-
Immutable.
-
s[start:end:step]
-
s[::-1] -
index() throws error; find() returns -1.
-
Loop & count aeious.
-
Compare with reverse.
-
Negative values index from end.
-
Loop and add if not present.
-
Optimization where identical strings share memory.
-
New string created → copying cost.
-
Using split().
-
Using join().
-
Sort both strings or use Counter.
-
split() + len().
-
isnumeric() supports more Unicode digits.
-
Using Counter.
-
== compares value, is compares identity.
-
Strings immutable → slice creates new string.
-
list(s)
ALSO CHECK
ALSO CHECK
.png)
Comments
Post a Comment