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

Listing 1: The 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).
(a) Functions to clean and format strings.
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 result
(b) Example use of the functions to format a Turkish name for folder naming.
def 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()

In Figure 1, various sequences show possible outputs of test_format_folder_name.

"Çiğdem Yildiz!       "
"Çiğdem_Yildiz!       "
"Cigdem_Yildiz!       "
"cigdem_yildiz!       "
"cigdem_yildiz       "
"cigdem_yildiz"
"cigdem_yildiz"
(a)
"Çiğdem Yildiz!       "
"Çiğdem_Yildiz!_______"
"Cigdem_Yildiz!_______"
"cigdem_yildiz!_______"
"cigdem_yildiz_______"
"cigdem_yildiz"
(b)
"Çiğdem Yildiz!       "
"Çiğdem Yildiz!"
"Çiğdem Yildiz"
"çiğdem yildiz"
"cigdem yildiz"
"cigdem_yildiz"
(c)
"Çiğdem Yildiz!       "
"Çiğdem Yildiz!"
"Çiğdem Yildiz"
"çiğdem yildiz"
"cigdem yildiz"
"cigdem_yildiz"
"cigdem_yildiz"
(d)
Figure 1: Alternative outputs of the 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! "?

  • text.strip() takes no arguments here and returns a string with whitespace removed from both ends, preserving internal spaces. " red blue ".strip() is "red blue"; an all-whitespace string becomes "". The original string is unchanged.
  • text.replace(old, new) returns a string with every occurrence of old replaced by new. "red-red".replace("red", "blue") is "blue-blue"; "red".replace("x", "") is "red". An empty replacement removes matches. The original string is unchanged.
  • text.lower() takes no arguments and returns the text with cased letters converted to lowercase. "Blue 7!".lower() is "blue 7!"; digits, punctuation, and an empty string stay unchanged. The original string is unchanged.
  • print(value) displays the value followed by a new line and returns None. print("Blue") displays Blue. With multiple arguments, the default separator is one space; print(2, 5) displays 2 5.