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