Interview

10 String Manipulation Python Interview Questions and Answers

Prepare for your Python interview with our guide on string manipulation techniques, featuring common questions and detailed answers.

String manipulation is a fundamental aspect of Python programming, essential for tasks ranging from data cleaning to user input processing. Python’s robust set of built-in methods and functions makes it particularly powerful for handling and transforming string data efficiently. Mastery of string manipulation techniques is crucial for writing clean, efficient, and effective code.

This article offers a curated selection of interview questions focused on string manipulation in Python. By working through these examples, you will gain a deeper understanding of key concepts and be better prepared to demonstrate your proficiency in this critical area during technical interviews.

String Manipulation Python Interview Questions and Answers

1. Write a function that takes two strings and returns their concatenation, but only if both strings have an even length. If either string has an odd length, return an empty string.

To solve this problem, we need to check the length of both input strings. If both strings have an even length, we concatenate them and return the result. If either string has an odd length, we return an empty string.

def concatenate_if_even_length(str1, str2):
    if len(str1) % 2 == 0 and len(str2) % 2 == 0:
        return str1 + str2
    else:
        return ""

# Examples
print(concatenate_if_even_length("test", "case"))  # Output: "testcase"
print(concatenate_if_even_length("hello", "world"))  # Output: ""

2. Given a string, write a function to replace all occurrences of a specified substring with another substring.

String manipulation often involves replacing occurrences of a specified substring with another. Python’s replace method is useful for this task. It takes two arguments: the substring to be replaced and the new substring, returning a new string with all occurrences replaced.

Example:

def replace_substring(original_string, old_substring, new_substring):
    return original_string.replace(old_substring, new_substring)

# Example usage
text = "Hello, world! Hello, everyone!"
new_text = replace_substring(text, "Hello", "Hi")
print(new_text)
# Output: "Hi, world! Hi, everyone!"

3. Write a function to find the first occurrence of a substring in a given string and return its index. If the substring is not found, return -1.

To find the first occurrence of a substring in a string and return its index, use Python’s find() method. It returns the lowest index of the substring if found, or -1 if not.

Example:

def find_substring(main_string, sub_string):
    return main_string.find(sub_string)

# Example usage
index = find_substring("hello world", "world")
print(index)  # Output: 6

index = find_substring("hello world", "python")
print(index)  # Output: -1

4. Use regular expressions to extract all email addresses from a given string.

Regular expressions (regex) are a powerful tool for pattern matching and string manipulation. They are ideal for tasks such as extracting email addresses. The re module in Python supports regex operations.

Example:

import re

def extract_emails(text):
    email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
    emails = re.findall(email_pattern, text)
    return emails

sample_text = "Please contact us at [email protected] or [email protected] for more information."
emails = extract_emails(sample_text)
print(emails)
# Output: ['[email protected]', '[email protected]']

5. Write a function to parse a log entry string and extract the timestamp, log level, and message.

To parse a log entry string and extract the timestamp, log level, and message, use Python’s string manipulation methods. Log entries typically follow a consistent format, making it easier to split the string and extract the required components.

Example:

def parse_log_entry(log_entry):
    parts = log_entry.split(' ', 2)
    timestamp = parts[0]
    log_level = parts[1].strip('[]')
    message = parts[2]
    return timestamp, log_level, message

log_entry = "2023-10-01 12:00:00 [INFO] This is a log message."
timestamp, log_level, message = parse_log_entry(log_entry)

print("Timestamp:", timestamp)
print("Log Level:", log_level)
print("Message:", message)

6. Discuss strategies for optimizing string concatenation in Python, especially in scenarios involving large numbers of strings.

Optimizing string concatenation in Python is important when dealing with many strings. Strategies include using the + operator, join() method, and list comprehensions.

  • Using the + Operator: This is straightforward but can be inefficient in loops due to the creation of new string objects at each iteration.
  • Using the join() Method: This method is generally more efficient for concatenating multiple strings, as it constructs a single string from an iterable.
  • Using List Comprehensions: Building a list of strings and then using join() to concatenate them minimizes the number of intermediate string objects created.

Example:

# Using the + operator
result = ""
for s in list_of_strings:
    result += s

# Using the join() method
result = "".join(list_of_strings)

# Using list comprehensions
result = "".join([s for s in list_of_strings])

7. Create a custom function that reverses the words in a sentence while maintaining their original order.

To reverse the words in a sentence while maintaining their original order, split the sentence into words, reverse each word, and then join them back together.

Example:

def reverse_words_in_sentence(sentence):
    words = sentence.split()
    reversed_words = [word[::-1] for word in words]
    return ' '.join(reversed_words)

sentence = "Hello world"
print(reverse_words_in_sentence(sentence))
# Output: "olleH dlrow"

8. Write a function to manipulate multiline strings, such as extracting lines containing a specific keyword.

String manipulation in Python is common, especially with multiline strings. To extract lines containing a specific keyword, split the string into lines and use list comprehensions for a clean solution.

Example:

def extract_lines_with_keyword(multiline_string, keyword):
    lines = multiline_string.split('\n')
    return [line for line in lines if keyword in line]

multiline_string = """This is a test string.
It contains multiple lines.
Some lines contain the keyword.
Keyword is important."""

keyword = "keyword"
result = extract_lines_with_keyword(multiline_string, keyword)
print(result)
# Output: ['Some lines contain the keyword.', 'Keyword is important.']

9. Design a function to find all overlapping occurrences of a substring within a string.

To find all overlapping occurrences of a substring within a string, use a sliding window approach. This involves iterating through the string and checking for the substring at each position, even if it overlaps with a previous occurrence.

Example:

def find_overlapping_substrings(s, sub):
    positions = []
    start = 0
    while start < len(s):
        pos = s.find(sub, start)
        if pos == -1:
            break
        positions.append(pos)
        start = pos + 1
    return positions

# Example usage
s = "ababa"
sub = "aba"
print(find_overlapping_substrings(s, sub))
# Output: [0, 2]

10. Write a function to remove all punctuation from a string and convert it to lowercase.

To remove all punctuation from a string and convert it to lowercase, use Python’s built-in string methods and the string module. The string module provides a list of punctuation characters, which can be used to filter out unwanted characters. Additionally, the lower() method converts the string to lowercase.

Example:

import string

def remove_punctuation_and_lowercase(input_str):
    translator = str.maketrans('', '', string.punctuation)
    return input_str.translate(translator).lower()

input_str = "Hello, World! This is a test."
result = remove_punctuation_and_lowercase(input_str)
print(result)
# Output: "hello world this is a test"
Previous

15 ABAP Interview Questions and Answers

Back to Interview
Next

10 WebFlux Interview Questions and Answers