Advertisement
Python253

to_title_case

May 23rd, 2024
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: to_title_case.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    This script converts input text to Title Case, where the first letter of each word is capitalized.
  10.    The converted text is then saved to a file named "converted_title_case.txt" in UTF-8 encoding.
  11.  
  12. Requirements:
  13.    - Python 3.x
  14.  
  15. Functions:
  16.    - to_title_case(text): Converts input text to Title Case.
  17.        Parameters:
  18.            text (str): The input text to be converted.
  19.        Returns:
  20.            str: The text converted to Title Case.
  21.  
  22. Usage:
  23.    Run the script and provide the text you want to convert when prompted.
  24.    The converted text will be saved to "converted_title_case.txt" in the same directory as the script.
  25.  
  26. Example Output:
  27.    Input Text: "this is a test of case converting"
  28.    
  29.    Converted Text Has Been Saved To: 'converted_title_case.txt'.
  30.    
  31.    Converted Text:
  32.            This Is A Test Of Case Converting
  33. """
  34.  
  35. def to_title_case(text):
  36.     """
  37.    Converts input text to Title Case.
  38.    
  39.    Parameters:
  40.        text (str): The input text to be converted.
  41.    
  42.    Returns:
  43.        str: The text converted to Title Case.
  44.    """
  45.     words = text.split()
  46.     title_case_words = [word.capitalize() for word in words]
  47.     return ' '.join(title_case_words)
  48.  
  49. def main():
  50.     """
  51.    Main function of the script.
  52.    
  53.    Prompts the user to input text, converts it to Title Case,
  54.    and saves the converted text to a file named "converted_title_case.txt".
  55.    """
  56.     input_text = input("Enter the text you want to convert to Title Case: ")
  57.     title_case_with_spaces_text = to_title_case(input_text)
  58.  
  59.     with open("converted_title_case.txt", "w", encoding="utf-8") as file:
  60.         file.write(title_case_with_spaces_text)
  61.    
  62.     print(f"\nConverted Text Has Been Saved To: 'converted_title_case.txt'.\n\nConverted Text:\n\t\t{title_case_with_spaces_text}")
  63.  
  64. if __name__ == "__main__":
  65.     main()
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement