This Python program is an implementation to CS50’s Introduction to Programming with Python Week 2 - camelCase Problem Set, named camel.py
, helps you convert variable names from camel case to snake case. In some programming languages, camel case is used to name variables where the first word is lowercase and subsequent words start with uppercase letters. Snake case, on the other hand, uses underscores (_) to separate words, and all letters are in lowercase. This program takes a variable name in camel case as input and outputs the corresponding name in snake case.
-
Open your terminal.
-
Navigate to the directory where you have saved the
camel.py
file.cd path/to/your/directory
-
Run the program using the Python interpreter:
python camel.py
-
The program will prompt you to enter a variable name in camel case. After you press Enter, it will output the variable name in snake case.
# camel.py
def main():
camelCase = input("camelCase: ")
snake_case = convert_to_snake(camelCase)
print(f"snake_case: {snake_case}")
def convert_to_snake(camel):
snake_case = ""
for i in camel:
if i == i.upper():
i = i.replace(i, f"_{i.lower()}")
snake_case += i
return snake_case
if __name__ == "__main__":
main()
- Run the program as mentioned in the "How to Run the Program" section.
- Enter various variable names in camel case (e.g.,
firstName
,preferredFirstName
) as prompted by the program. - The program should convert the camel case variable names to snake case and output the corresponding names.
-
camelCase: name Result: snake_case: name
-
camelCase: firstName Result: snake_case: first_name
-
camelCase: preferredFirstName Result: snake_case: preferred_first_name
Remember to save the camel.py
file in the same directory where you are running the program. If you encounter any issues with the program not being found or not running as expected, make sure you are in the correct directory and have saved the file with the correct name.