Advertisement
Python253

to_camel_case

May 23rd, 2024
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: to_camel_case.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    This script converts input text to Camel Case, where the case of each letter alternates,
  10.    starting with uppercase for the first letter. Spaces and other non-alphabetic characters
  11.    are preserved in the original position.
  12.    
  13.    The converted text is then saved to a file named "converted_camel_case.txt" in UTF-8 encoding.
  14.  
  15. Requirements:
  16.    - Python 3.x
  17.  
  18. Functions:
  19.    - to_camel_case(text): Converts input text to Camel Case.
  20.        Parameters:
  21.            text (str): The input text to be converted.
  22.        Returns:
  23.            str: The text converted to Camel Case.
  24.  
  25. Usage:
  26.    Run the script and provide the text you want to convert when prompted.
  27.    The converted text will be saved to "converted_camel_case.txt" in the same directory as the script.
  28.  
  29. Example Output:
  30.    Input Text: "This is a Test of Case Converting"
  31.    
  32.    Converted Text Has Been Saved To: 'converted_camel_case.txt'.
  33.    
  34.    Converted Text:
  35.            ThIs Is A TeSt Of CaSe CoNvErTiNg
  36. """
  37.  
  38. def to_camel_case(text):
  39.     """
  40.    Convert a given string to Camel Case where each letter's case alternates,
  41.    starting with uppercase.
  42.    
  43.    Parameters:
  44.        text (str): The input string.
  45.        
  46.    Returns:
  47.        str: The Camel Case converted string.
  48.    """
  49.     result = []
  50.     upper = True
  51.  
  52.     for char in text:
  53.         if char.isalpha():
  54.             if upper:
  55.                 result.append(char.upper())
  56.             else:
  57.                 result.append(char.lower())
  58.             upper = not upper
  59.         else:
  60.             result.append(char)
  61.  
  62.     return ''.join(result)
  63.  
  64. def main():
  65.     """
  66.    Main function to prompt user input and convert the text to Camel Case.
  67.    The converted text is saved to a file.
  68.    """
  69.     input_text = input("Enter the text you want to convert to Camel Case: ")
  70.     camel_case_text = to_camel_case(input_text)
  71.    
  72.     with open("converted_camel_case.txt", "w", encoding="utf-8") as file:
  73.         file.write(camel_case_text)
  74.    
  75.     print("\nConverted Text Has Been Saved To: 'converted_camel_case.txt'.\n")
  76.     print("Converted Text:\n")
  77.     print(f"\t\t{camel_case_text}")
  78.  
  79. if __name__ == "__main__":
  80.     main()
  81.  
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement