M6) Folder-Friendly Format
Turkish names often contain special characters and diacritics that can cause issues when used for folder naming.
In Listing 1 (a), we provide a set of Python functions to clean, format, and adjust Turkish names for folder naming conventions by removing leading and trailing spaces, converting to lowercase, replacing punctuation and special characters, and adding underscores.
In Listing 1 (b), we demonstrate the usage of these functions to format a Turkish name for folder naming. Executing the test_format_folder_name function on the input "Çiğdem Yildiz! " (where the exclamation mark and trailing spaces are part of the input) transforms it into a clean, folder-friendly format, with each intermediate step printed in the terminal.
In Figure 1, various sequences show possible outputs of test_format_folder_name.
test_format_folder_name function for the input "Çiğdem Yildiz! ".
Which output sequence in Figure 1 corresponds to the correct result for test_format_folder_name shown in Listing 1 (b) after formatting the input "Çiğdem Yildiz! "?
In Python, a str is a sequence of characters. Key string methods and functions used include:
strip: Removes leading and trailing whitespace. Syntax:text.strip().replace: Substitutes a specific substring within a string. Syntax:text.replace(target, replacement).lower: Converts all cased characters in a string to lowercase. Syntax:text.lower().print: Outputs text to the terminal. Syntax:print(output_text).
test_format_folder_name function calls format_folder_name to format a Turkish name for use in folder naming. The format_folder_name function calls add_underscores, to_regular, to_lower, strip_punct, and trim_spaces in a nested expression. Python evaluates the innermost call first, so the intermediate results are printed to the terminal from trim_spaces through add_underscores (Question 1).
def trim_spaces(text: str) -> str:
"""Remove leading and trailing whitespace."""
result = text.strip()
print(f'"{result}"')
return result
def strip_punct(text: str) -> str:
"""Remove punctuation symbols."""
result = text.replace("!", "")
result = result.replace("?", "")
result = result.replace(".", "")
result = result.replace(",", "")
print(f'"{result}"')
return result
def to_lower(text: str) -> str:
"""Convert the text to lowercase."""
result = text.lower()
print(f'"{result}"')
return result
def to_regular(text: str) -> str:
"""Replace special characters."""
result = text.replace("ç", "c")
result = result.replace("Ç", "C")
result = result.replace("ğ", "g")
print(f'"{result}"')
return result
def add_underscores(text: str) -> str:
"""Replace spaces with underscores."""
result = text.replace(" ", "_")
print(f'"{result}"')
return resultdef format_folder_name(text: str) -> str:
"""Prepare a name for folder format."""
return add_underscores(
to_regular(
to_lower(
strip_punct(
trim_spaces(text)
)
)
)
)
def test_format_folder_name() -> None:
name_text = "Çiğdem Yildiz! "
print(f'"{name_text}"')
print(f'"{format_folder_name(name_text)}"')
if __name__ == "__main__":
test_format_folder_name()