Advertisement
Python253

text_case_converter

May 23rd, 2024 (edited)
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.21 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: text_case_converter.py
  4. # Version: 1.0.1
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    This script offers functionality to convert text between various case formats including Snake-Case, Title-Case, Kebab-Case, Pascal-Case, and Camel-Case.
  10.  
  11. Requirements:
  12.    - Python 3.x
  13.    - No additional dependencies required.
  14.  
  15. Functions:
  16.    - to_snake_case(text):
  17.        Converts text to Snake-Case format.
  18.        Parameters:
  19.            text (str): The input text to be converted.
  20.        Returns:
  21.            str: The text converted to Snake-Case format.
  22.    
  23.    - to_title_case(text):
  24.        Converts text to Title-Case format.
  25.        Parameters:
  26.            text (str): The input text to be converted.
  27.        Returns:
  28.            str: The text converted to Title-Case format.
  29.  
  30.    - to_kebab_case(text):
  31.        Converts text to Kebab-Case format.
  32.        Parameters:
  33.            text (str): The input text to be converted.
  34.        Returns:
  35.            str: The text converted to Kebab-Case format.
  36.  
  37.    - to_pascal_case(text):
  38.        Converts text to Pascal-Case format.
  39.        Parameters:
  40.            text (str): The input text to be converted.
  41.        Returns:
  42.            str: The text converted to Pascal-Case format.
  43.  
  44.    - to_camel_case(text):
  45.        Converts text to Camel-Case format.
  46.        Parameters:
  47.            text (str): The input text to be converted.
  48.        Returns:
  49.            str: The text converted to Camel-Case format.
  50.  
  51. Usage:
  52.    - Call the main() function to execute the script.
  53.    - Follow the prompts to choose the conversion option and enter the text to be converted.
  54.    - The converted text will be saved to a file in the current working directory.
  55.  
  56. Example Output:
  57.    Input Text: "this is a test of case converting"
  58.  
  59.        1: Snake-Case:
  60.           - Output: "this_is_a_test_of_case_converting"
  61.        
  62.        2: Title-Case:
  63.           - Output: "This Is A Test Of Case Converting"
  64.        
  65.        3: Kebab-Case:
  66.           - Output: "this-is-a-test-of-case-converting"
  67.        
  68.        4: Pascal-Case:
  69.           - Output: "ThisIsATestOfCaseConverting"
  70.        
  71.        5: Camel-Case:
  72.           - Output: "ThIs Is A TeSt Of CaSe CoNvErTiNg"
  73.    
  74. Additional Notes:
  75.    - The script allows for easy conversion between different case formats commonly used in programming and text formatting.
  76.    - The file name for the converted text will be based on the chosen conversion option.
  77. """
  78.  
  79. def to_snake_case(text):
  80.     """
  81.    Converts text to Snake-Case format.
  82.    
  83.    Parameters:
  84.        text (str): The input text to be converted.
  85.    
  86.    Returns:
  87.        str: The text converted to Snake-Case format.
  88.    """
  89.     return '_'.join(text.lower().split())
  90.  
  91. def to_title_case(text):
  92.     """
  93.    Converts text to Title-Case format.
  94.    
  95.    Parameters:
  96.        text (str): The input text to be converted.
  97.    
  98.    Returns:
  99.        str: The text converted to Title-Case format.
  100.    """
  101.     return ' '.join(word.capitalize() for word in text.split())
  102.  
  103. def to_kebab_case(text):
  104.     """
  105.    Converts text to Kebab-Case format.
  106.    
  107.    Parameters:
  108.        text (str): The input text to be converted.
  109.    
  110.    Returns:
  111.        str: The text converted to Kebab-Case format.
  112.    """
  113.     return '-'.join(text.lower().split())
  114.  
  115. def to_pascal_case(text):
  116.     """
  117.    Converts text to Pascal-Case format.
  118.    
  119.    Parameters:
  120.        text (str): The input text to be converted.
  121.    
  122.    Returns:
  123.        str: The text converted to Pascal-Case format.
  124.    """
  125.     words = text.split()
  126.     pascal_case_words = [word.capitalize() for word in words]
  127.     return ''.join(pascal_case_words)
  128.  
  129. def to_camel_case(text):
  130.     """
  131.    Convert a given string to Camel Case where each letter's case alternates,
  132.    starting with uppercase.
  133.    
  134.    Parameters:
  135.        text (str): The input string.
  136.        
  137.    Returns:
  138.        str: The Camel Case converted string.
  139.    """
  140.     result = []
  141.     upper = True
  142.  
  143.     for char in text:
  144.         if char.isalpha():
  145.             if upper:
  146.                 result.append(char.upper())
  147.             else:
  148.                 result.append(char.lower())
  149.             upper = not upper
  150.         else:
  151.             result.append(char)
  152.  
  153.     return ''.join(result)
  154.  
  155. def main():
  156.     """
  157.    Main function of the script.
  158.    
  159.    Prompts the user to choose a conversion option and input text to be converted.
  160.    Executes the selected conversion function and saves the converted text to a file.
  161.    """
  162.     menu = """
  163. Choose a case conversion option below:
  164.  
  165.            1: Snake-Case
  166.            2: Title-Case
  167.            3: Kebab-Case
  168.            4: Pascal-Case
  169.            5: Camel-Case
  170.            0: Exit
  171.    """
  172.  
  173.     while True:
  174.         print(menu)
  175.         choice = input("Enter your choice (1-5) or press 0 to exit: ")
  176.  
  177.         if choice == '0':
  178.             print("\nExiting Program...   Goodbye!\n")
  179.             break
  180.  
  181.         if choice not in {'1', '2', '3', '4', '5'}:
  182.             print("\nInvalid choice!\nPlease enter a number from 1 to 5 or press 0 to exit.")
  183.             continue
  184.  
  185.         input_text = input("\nEnter the text you want to convert: ")
  186.  
  187.         if choice == '1':
  188.             converted_text = to_snake_case(input_text)
  189.             filename = "converted_snake_case.txt"
  190.         elif choice == '2':
  191.             converted_text = to_title_case(input_text)
  192.             filename = "converted_title_case.txt"
  193.         elif choice == '3':
  194.             converted_text = to_kebab_case(input_text)
  195.             filename = "converted_kebab_case.txt"
  196.         elif choice == '4':
  197.             converted_text = to_pascal_case(input_text)
  198.             filename = "converted_pascal_case.txt"
  199.         elif choice == '5':
  200.             converted_text = to_camel_case(input_text)
  201.             filename = "converted_camel_case.txt"
  202.  
  203.         with open(filename, "w", encoding="utf-8") as file:
  204.             file.write(converted_text)
  205.        
  206.         print(f"\nConverted Text Has Been Saved To:\n\n\t\t'{filename}'.\n\nConverted Text:\n\n\t\t{converted_text}")
  207.  
  208.         input("\nPress Enter to return to the main menu...")
  209.  
  210. if __name__ == "__main__":
  211.     main()
  212.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement