Advertisement
Python253

text_case_converter

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