camelCase
Source: en.wikipedia.org/wiki/Camel_case
In some languages, it’s common to use camel case (otherwise known as “mixed case”) for variables’ names when those names comprise multiple words, whereby the first letter of the first word is lowercase but the first letter of each subsequent word is uppercase. For instance, whereas a variable for a user’s name might be called name
, a variable for a user’s first name might be called firstName
, and a variable for a user’s preferred first name (e.g., nickname) might be called preferredFirstName
.
Python, by contrast, recommends snake case, whereby words are instead separated by underscores (_), with all letters in lowercase. For instance, those same variables would be called name
, first_name
, and preferred_first_name
, respectively, in Python.
In a file called camel.py
, implement a program that prompts the user for the name of a variable in camel case and outputs the corresponding name in snake case. Assume that the user’s input will indeed be in camel case.
Hints
- Recall that a
str
comes with quite a few methods, per docs.python.org/3/library/stdtypes.html#string-methods. - Much like a
list
, astr
is “iterable,” which means you can iterate over each of its characters in a loop. For instance, ifs
is astr
, you could print each of its characters, one at a time, with code like:
Before You Begin
From the root of your repository execute cd src/2-Loops
So your current working directory is ...
camel.py
where you’ll write your program.
Success
Your program must have a function called camel_to_snake
that takes a string as input and returns a new string with all of its camelCase words converted to snake_case. Your main
function must call this function with a user input string and print the result.
How to Test
Here’s how to test your code manually:
- Run your program with
python camel.py
. Typename
and press Enter. Your program should output: - Run your program with
python camel.py
. TypefirstName
and press Enter. Your program should output: - Run your program with
python camel.py
. TypepreferredFirstName
and press Enter. Your program should output
Pytest
You can execute the below to check your code using pytest
from the root directory.
A green output from running the test means it was successful. A red output means there is a bug in your code that you need to fix.
How to Submit
From github desktop or the command line, commit your changes and push them to your repository.
Codespaces
If you are using codespaces, you can commit your changes directly from the Codespace interface. Click on the Source Control icon in the left sidebar, then click on the "..." button and select "Commit to main". Enter a commit message and click "Commit".
Codespace terminal or your local terminal.
Note
You will need to have installed git-scm
for this to work locally
At the /datatypes $
prompt in your terminal:
Note
Remember to replace "your message here" with a meaningful commit message that describes your changes.