Python Strings & Built-in Functions

Python Strings & Built-in Functions Interview Questions

What is a string in Python?
A string in Python is a sequence of Unicode characters. Strings are immutable and can be created using single quotes ('hello'), double quotes ("hello"), or triple quotes for multi-line strings. Example: name = "Python".
What is the len() function for strings?
len() returns the number of characters in a string. Example: len("Hello") returns 5. It counts all characters including spaces and special characters.
What is the str() function?
The str() function converts any Python object to a string representation. Example: str(123) returns "123", str([1,2,3]) returns "[1, 2, 3]".
What does the upper() method do?
upper() converts all characters in a string to uppercase. Example: "hello".upper() returns "HELLO". It doesn't modify the original string (strings are immutable).
What does the lower() method do?
lower() converts all characters in a string to lowercase. Example: "HELLO".lower() returns "hello". Useful for case-insensitive comparisons.
What is the strip() method?
strip() removes leading and trailing whitespace (spaces, tabs, newlines). lstrip() removes leading whitespace only. rstrip() removes trailing whitespace only. Example: " hello ".strip() returns "hello".
What does the split() method do?
split() divides a string into a list of substrings based on a delimiter. Default delimiter is whitespace. Example: "apple,banana,cherry".split(",") returns ["apple", "banana", "cherry"].
What does the join() method do?
join() concatenates elements of an iterable (like a list) into a single string, with the string as separator. Example: ",".join(["a", "b", "c"]) returns "a,b,c".
What does the replace() method do?
replace(old, new[, count]) returns a copy of the string with occurrences of old replaced by new. Optional count limits replacements. Example: "hello".replace("l", "x") returns "hexxo".
What does the find() method do?
find(sub[, start[, end]]) returns the lowest index where substring sub is found, or -1 if not found. Example: "hello".find("l") returns 2. rfind() searches from the right.
What does the startswith() method do?
startswith(prefix[, start[, end]]) returns True if string starts with prefix, otherwise False. Example: "hello".startswith("he") returns True. endswith() checks the end of the string.
What does the count() method do?
count(sub[, start[, end]]) returns the number of non-overlapping occurrences of substring sub. Example: "hello".count("l") returns 2. It's case-sensitive.
What is the capitalize() method?
capitalize() returns a copy of the string with its first character capitalized and the rest lowercased. Example: "hello WORLD".capitalize() returns "Hello world".
What is the title() method?
title() returns a titlecased version where words start with uppercase and remaining characters are lowercase. Example: "hello world".title() returns "Hello World".
What is the swapcase() method?
swapcase() returns a copy with uppercase characters converted to lowercase and vice versa. Example: "Hello World".swapcase() returns "hELLO wORLD".
What are isalpha(), isdigit(), and isalnum() methods?
isalpha(): Returns True if all characters are alphabetic.
isdigit(): Returns True if all characters are digits.
isalnum(): Returns True if all characters are alphanumeric.
Example: "abc".isalpha() = True, "123".isdigit() = True, "abc123".isalnum() = True.
What is the format() method?
format() formats strings by replacing placeholders {} with values. Example: "Hello {}".format("World") returns "Hello World". It supports positional and keyword arguments.
What does the index() method do?
index(sub[, start[, end]]) like find(), but raises ValueError if substring not found. Example: "hello".index("l") returns 2. rindex() searches from the right.
What is the partition() method?
partition(sep) splits string at first occurrence of sep, returns 3-tuple: (part before sep, sep, part after sep). Example: "hello.world".partition(".") returns ("hello", ".", "world").
What is the zfill() method?
zfill(width) pads numeric string with zeros on the left to reach specified width. Example: "42".zfill(5) returns "00042". Useful for formatting numbers with leading zeros.
Why does building a string with += in a loop worry big-O pedants?

CPython often optimizes repeated concatenation in some cases, but worst-case narrative remains O(n²) copies—''.join(parts) is the robust O(n) pattern.

"a" * 3 vs repeated concatenation?

Multiplication builds one new string efficiently—clear for fixed repetition.

str.split() with no argument—what splits?

Default split() splits on any run of whitespace and trims edges; splitting on a literal space is different.

"  a  b  c  ".split()
# ['a', 'b', 'c']

"  a  b  c  ".split(' ')
# ['', '', 'a', '', 'b', '', 'c', '', '']
strip() removes substrings from the middle?

No—only leading/trailing characters in the optional charset (default whitespace).

find vs index when substring missing?

find returns -1; index raises ValueError—choose for control flow vs exceptions.

Why can a is b be True for two string literals?

Implementation-dependent interning—never rely on identity for string equality; always use ==.

f"{{ brace }}"—how to output literal braces?

Double the braces—single braces are expression boundaries in f-strings.

r"\n" raw string—still two chars?

Yes—backslash and n; raw mainly affects backslash escaping, not quote rules edge cases at string end.

"café" length—bytes vs characters?

len counts Unicode code points—combining characters and emoji may surprise perceived “character” count; use unicodedata or grapheme libs if needed.

str vs bytes—encoding pitfall?

Decode bytes with explicit encoding (UTF-8 default in many contexts)—silent wrong decoding corrupts text; use errors= policy.

casefold() vs lower()?

casefold is stronger for case-insensitive comparison across locales—prefer for matching; lower for display rules.

startswith(( "http://", "https://" ))—allowed?

Tuple of prefixes tests any—cleaner than chained or calls.

partition vs split(..., 1)?

partition always returns 3-tuple including separator—predictable when sep may be absent.

"%s" % (x,)—why the comma?

Single-element tuple needs trailing comma—otherwise (x) is just parentheses, not tuple, breaking formatting.

replace(old, new, count)—first occurrences?

Replaces left-to-first count matches—order matters for overlapping patterns (non-overlapping scan).

Slicing past end—safe?

s[:1000] won’t raise on short strings—clamps—unlike single-index access.

title() vs capitalize()?

title capitalizes each word (naïve); capitalize only first char of whole string—both locale-insensitive simple rules.

splitlines(keepends=True)—why?

Preserves \n/\r\n boundaries—line parsers needing delimiter info.

removeprefix / removesuffix (3.9+) behavior?

Removes one occurrence at start/end if present—safer than blind slice lengths.

str.center(10, '-')—padding rules?

Extra space favors left padding first when odd—document asymmetry if alignment matters in UI.

Why regex sometimes slower than string methods?

Fixed substring search uses optimized algorithms (e.g. Boyer-Moore-ish)—regex engine overhead unnecessary for literal needles.

isdigit() vs isnumeric() vs isdecimal()?

Strictness ladder differs—Unicode fractions may be numeric but not decimal; interviews test awareness of edge Unicode classes.

format_spec: {:,} meaning?

Thousands separator per locale—readable large integers in formatted output.

Immutability—can you “change” a string?

Rebinding name to new object only—methods return new strings; explains safe dict keys if content frozen.

join on non-string elements?

TypeError—map str first; generator expressions common for large sequences.

translate with str.maketrans—use case?

Bulk character replacement/delete faster than many replace calls—classic ASCII cleanup pipelines.

Empty string truthiness?

Falsy—if not s: catches empty; whitespace-only strings are truthy unless stripped.

Why normalize Unicode before comparing filenames?

Same visual glyph can encode multiple ways (NFC vs NFD)—security and duplicate detection bugs otherwise.

lstrip with string argument—prefix strip?

Removes any combination of leading chars from the set—not whole prefix substring—surprise for multi-char “prefix” expectations.

Final: textwrap.shorten vs slice?

Adds placeholder and breaks at word boundaries—better UX than hard ellipsis slices.

Note: These are fundamental string methods in Python. Remember that strings are immutable - all these methods return new strings rather than modifying the original. Most string methods have optional start and end parameters to limit the operation to a substring. Understanding these methods is crucial for effective string manipulation in Python.
Prev: Arrays Python Strings & Built-in Functions Next: Recursion