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).

Pages: 1 2 3 4 5 6


Discover more from HintsToday

Subscribe to get the latest posts sent to your email.

Posted in

Discover more from HintsToday

Subscribe now to keep reading and get access to the full archive.

Continue reading