Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: text_case_converter.py
- # Version: 1.0.1
- # Author: Jeoi Reqi
- """
- Description:
- This script offers functionality to convert text between various case formats including Snake-Case, Title-Case, Kebab-Case, Pascal-Case, and Camel-Case.
- Requirements:
- - Python 3.x
- - No additional dependencies required.
- Functions:
- - to_snake_case(text):
- Converts text to Snake-Case format.
- Parameters:
- text (str): The input text to be converted.
- Returns:
- str: The text converted to Snake-Case format.
- - to_title_case(text):
- Converts text to Title-Case format.
- Parameters:
- text (str): The input text to be converted.
- Returns:
- str: The text converted to Title-Case format.
- - to_kebab_case(text):
- Converts text to Kebab-Case format.
- Parameters:
- text (str): The input text to be converted.
- Returns:
- str: The text converted to Kebab-Case format.
- - to_pascal_case(text):
- Converts text to Pascal-Case format.
- Parameters:
- text (str): The input text to be converted.
- Returns:
- str: The text converted to Pascal-Case format.
- - to_camel_case(text):
- Converts text to Camel-Case format.
- Parameters:
- text (str): The input text to be converted.
- Returns:
- str: The text converted to Camel-Case format.
- Usage:
- - Call the main() function to execute the script.
- - Follow the prompts to choose the conversion option and enter the text to be converted.
- - The converted text will be saved to a file in the current working directory.
- Example Output:
- Input Text: "this is a test of case converting"
- 1: Snake-Case:
- - Output: "this_is_a_test_of_case_converting"
- 2: Title-Case:
- - Output: "This Is A Test Of Case Converting"
- 3: Kebab-Case:
- - Output: "this-is-a-test-of-case-converting"
- 4: Pascal-Case:
- - Output: "ThisIsATestOfCaseConverting"
- 5: Camel-Case:
- - Output: "ThIs Is A TeSt Of CaSe CoNvErTiNg"
- Additional Notes:
- - The script allows for easy conversion between different case formats commonly used in programming and text formatting.
- - The file name for the converted text will be based on the chosen conversion option.
- """
- def to_snake_case(text):
- """
- Converts text to Snake-Case format.
- Parameters:
- text (str): The input text to be converted.
- Returns:
- str: The text converted to Snake-Case format.
- """
- return '_'.join(text.lower().split())
- def to_title_case(text):
- """
- Converts text to Title-Case format.
- Parameters:
- text (str): The input text to be converted.
- Returns:
- str: The text converted to Title-Case format.
- """
- return ' '.join(word.capitalize() for word in text.split())
- def to_kebab_case(text):
- """
- Converts text to Kebab-Case format.
- Parameters:
- text (str): The input text to be converted.
- Returns:
- str: The text converted to Kebab-Case format.
- """
- return '-'.join(text.lower().split())
- def to_pascal_case(text):
- """
- Converts text to Pascal-Case format.
- Parameters:
- text (str): The input text to be converted.
- Returns:
- str: The text converted to Pascal-Case format.
- """
- words = text.split()
- pascal_case_words = [word.capitalize() for word in words]
- return ''.join(pascal_case_words)
- def to_camel_case(text):
- """
- Convert a given string to Camel Case where each letter's case alternates,
- starting with uppercase.
- Parameters:
- text (str): The input string.
- Returns:
- str: The Camel Case converted string.
- """
- result = []
- upper = True
- for char in text:
- if char.isalpha():
- if upper:
- result.append(char.upper())
- else:
- result.append(char.lower())
- upper = not upper
- else:
- result.append(char)
- return ''.join(result)
- def main():
- """
- Main function of the script.
- Prompts the user to choose a conversion option and input text to be converted.
- Executes the selected conversion function and saves the converted text to a file.
- """
- menu = """
- Choose a case conversion option below:
- 1: Snake-Case
- 2: Title-Case
- 3: Kebab-Case
- 4: Pascal-Case
- 5: Camel-Case
- 0: Exit
- """
- while True:
- print(menu)
- choice = input("Enter your choice (1-5) or press 0 to exit: ")
- if choice == '0':
- print("\nExiting Program... Goodbye!\n")
- break
- if choice not in {'1', '2', '3', '4', '5'}:
- print("\nInvalid choice!\nPlease enter a number from 1 to 5 or press 0 to exit.")
- continue
- input_text = input("\nEnter the text you want to convert: ")
- if choice == '1':
- converted_text = to_snake_case(input_text)
- filename = "converted_snake_case.txt"
- elif choice == '2':
- converted_text = to_title_case(input_text)
- filename = "converted_title_case.txt"
- elif choice == '3':
- converted_text = to_kebab_case(input_text)
- filename = "converted_kebab_case.txt"
- elif choice == '4':
- converted_text = to_pascal_case(input_text)
- filename = "converted_pascal_case.txt"
- elif choice == '5':
- converted_text = to_camel_case(input_text)
- filename = "converted_camel_case.txt"
- with open(filename, "w", encoding="utf-8") as file:
- file.write(converted_text)
- print(f"\nConverted Text Has Been Saved To:\n\n\t\t'{filename}'.\n\nConverted Text:\n\n\t\t{converted_text}")
- input("\nPress Enter to return to the main menu...")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement