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

MethodDescriptionExample
.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 4
    • s[:3] → from start to index 2
    • s[3:] → from index 3 to end
    • s[::-1] → reverse string
    • s[::2] → every 2nd char
    • s[-1] → last char
    • s[-2:] → last 2 chars

4. Tricky & Common Interview Coding Questions

🔹 Basic → Intermediate

  1. Reverse a string s = "hello" print(s[::-1]) # 'olleh'
  2. Check palindrome s = "madam" print(s == s[::-1]) # True
  3. Count vowels s = "programming" vowels = "aeiou" print(sum(1 for ch in s if ch in vowels)) # 3
  4. Remove duplicates but keep order s = "programming" result = "".join(dict.fromkeys(s)) print(result) # 'progamin'
  5. Anagram check from collections import Counter s1, s2 = "listen", "silent" print(Counter(s1) == Counter(s2)) # True
  6. First non-repeating character s = "aabbcdeff" for ch in s: if s.count(ch) == 1: print(ch) # 'c' break

🔹 Advanced / Tricky

  1. 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']
  2. 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')
  3. Check if one string is rotation of another s1, s2 = "abcd", "cdab" print(len(s1) == len(s2) and s2 in (s1+s1)) # True
  4. 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).