Perfect π β letβs build a complete notes + interview Q&A sheet for Python strings covering:
π Python String Notes (for Interviews)
1. String Basics
- Strings are immutable sequences of Unicode characters.
- Defined with single
'...'
, double"..."
, or triple quotes'''...''' / """..."""
.
2. Common String Methods
Method | Description | Example |
---|---|---|
.upper() | Convert to uppercase | "abc".upper() β "ABC" |
.lower() | Convert to lowercase | "ABC".lower() β "abc" |
.title() | Capitalize each word | "hello world".title() β "Hello World" |
.capitalize() | Capitalize first char | "python".capitalize() β "Python" |
.strip() | Remove whitespace (or chars) | " hi ".strip() β "hi" |
.lstrip() / .rstrip() | Strip left/right | " hi".lstrip() β "hi" |
.find("x") | First index of substring (-1 if not found) | "hello".find("l") β 2 |
.index("x") | Like find but raises error if not found | "hello".index("z") β ValueError |
.replace(a, b) | Replace substring | "hello".replace("l", "x") β "hexxo" |
.split() | Split into list | "a,b,c".split(",") β ['a','b','c'] |
.join() | Join list into string | ",".join(["a","b"]) β "a,b" |
.startswith("x") | Check prefix | "hello".startswith("he") β True |
.endswith("x") | Check suffix | "hello".endswith("lo") β True |
.isalpha() | Only letters? | "abc".isalpha() β True |
.isdigit() | Only digits? | "123".isdigit() β True |
.isalnum() | Letters or digits? | "abc123".isalnum() β True |
.count("x") | Count occurrences | "banana".count("a") β 3 |
3. String Slicing Techniques
- General form:
s[start:end:step]
s[2:5]
β substring from index 2 to 4s[:3]
β from start to index 2s[3:]
β from index 3 to ends[::-1]
β reverse strings[::2]
β every 2nd chars[-1]
β last chars[-2:]
β last 2 chars
4. Tricky & Common Interview Coding Questions
πΉ Basic β Intermediate
- Reverse a string
s = "hello" print(s[::-1]) # 'olleh'
- Check palindrome
s = "madam" print(s == s[::-1]) # True
- Count vowels
s = "programming" vowels = "aeiou" print(sum(1 for ch in s if ch in vowels)) # 3
- Remove duplicates but keep order
s = "programming" result = "".join(dict.fromkeys(s)) print(result) # 'progamin'
- Anagram check
from collections import Counter s1, s2 = "listen", "silent" print(Counter(s1) == Counter(s2)) # True
- First non-repeating character
s = "aabbcdeff" for ch in s: if s.count(ch) == 1: print(ch) # 'c' break
πΉ Advanced / Tricky
- Find all substrings of a string
s = "abc" subs = [s[i:j] for i in range(len(s)) for j in range(i+1, len(s)+1)] print(subs) # ['a', 'ab', 'abc', 'b', 'bc', 'c']
- Longest substring without repeating characters
s = "abcabcbb" seen, left, max_len = {}, 0, 0 for right, ch in enumerate(s): if ch in seen and seen[ch] >= left: left = seen[ch] + 1 seen[ch] = right max_len = max(max_len, right - left + 1) print(max_len) # 3 ('abc')
- Check if one string is rotation of another
s1, s2 = "abcd", "cdab" print(len(s1) == len(s2) and s2 in (s1+s1)) # True
- Find longest common prefix
strs = ["flower", "flow", "flight"] prefix = strs[0] for s in strs[1:]: while not s.startswith(prefix): prefix = prefix[:-1] print(prefix) # 'fl'
β These are most asked string questions in Python interviews (especially for data engineering / SDE roles).