Hone logo
Hone
Problems

Mastering Python String Manipulation

Python strings are fundamental data types used to represent textual information. Understanding how to effectively manipulate strings is a crucial skill for any programmer, enabling tasks like data cleaning, text analysis, and user interaction. This challenge will test your ability to implement common string operations using Python's built-in methods.

Problem Description

Your task is to create a Python function that takes a string as input and performs a series of transformations on it based on specific instructions. You will leverage Python's built-in string methods to achieve these transformations. The function should return the final transformed string.

Key Requirements:

  1. Convert to Lowercase: The input string must first be converted entirely to lowercase.
  2. Remove Leading/Trailing Whitespace: Any whitespace characters (spaces, tabs, newlines) at the beginning or end of the string should be removed.
  3. Replace Specific Substrings: Replace all occurrences of a given substring (e.g., "old_word") with another substring (e.g., "new_word").
  4. Count Occurrences: Count how many times a specific character appears in the processed string.
  5. Split into Words: Split the string into a list of individual words, using whitespace as the delimiter.
  6. Join Words with Dashes: Join the list of words back into a single string, but this time, use a hyphen (-) as the separator between words.

Expected Behavior:

The function should accept a string, along with two substrings for replacement and a character to count, and return the final processed string.

Edge Cases to Consider:

  • Empty input string.
  • String with only whitespace.
  • Substrings for replacement not present in the original string.
  • The character to count not present in the string.
  • Case sensitivity in replacement (though the first step handles this).

Examples

Example 1:

Input:
original_string = "  Hello World!  "
old_substring = "World"
new_substring = "Python"
char_to_count = "o"

Output:
"hello-python!-o-2"

Explanation:
1. "  Hello World!  " becomes "  hello world!  " (lowercase).
2. "  hello world!  " becomes "hello world!" (stripped).
3. "hello world!" becomes "hello python!" (replaced "world" with "python").
4. "hello python!" has "o" appear 2 times.
5. "hello python!" splits into ["hello", "python!"].
6. ["hello", "python!"] joins to "hello-python!".
7. The final output concatenates the joined string, a hyphen, and the count: "hello-python!-o-2".

Example 2:

Input:
original_string = "  Data SCIENCE is FUN!  "
old_substring = "SCIENCE"
new_substring = "analysis"
char_to_count = "a"

Output:
"data-analysis-is-fun!-a-2"

Explanation:
1. "  Data SCIENCE is FUN!  " becomes "  data science is fun!  ".
2. "  data science is fun!  " becomes "data science is fun!".
3. "data science is fun!" becomes "data analysis is fun!" (replacing "science" with "analysis").
4. "data analysis is fun!" has "a" appear 2 times.
5. "data analysis is fun!" splits into ["data", "analysis", "is", "fun!"].
6. ["data", "analysis", "is", "fun!"] joins to "data-analysis-is-fun!".
7. The final output is "data-analysis-is-fun!-a-2".

Example 3 (Edge Case):

Input:
original_string = ""
old_substring = "test"
new_substring = "prod"
char_to_count = "z"

Output:
"-z-0"

Explanation:
1. "" becomes "" (lowercase).
2. "" becomes "" (stripped).
3. "" remains "" (replace "test" with "prod" - no effect).
4. "" has "z" appear 0 times.
5. "" splits into [].
6. [] joins to "".
7. The final output is "-z-0".

Constraints

  • The input original_string will be a string.
  • The old_substring and new_substring will be strings.
  • The char_to_count will be a single-character string.
  • The length of original_string will not exceed 1000 characters.
  • The lengths of old_substring and new_substring will not exceed 50 characters.
  • The total number of operations should be efficient, utilizing built-in string methods.

Notes

  • You are expected to use Python's built-in string methods like .lower(), .strip(), .replace(), .count(), .split(), and .join().
  • The final output format should be the transformed string, followed by a hyphen, the character to count, another hyphen, and then the count of that character.
  • Consider how case sensitivity might affect your replacement if you weren't performing the lowercase conversion first.
Loading editor...
python