Home | Lecture 2 | Problem 2.1 | Problem 2.2 | Problem 2.3 | Problem 2.4 | Problem 2.5
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.
- Recall that a
strcomes with quite a few methods, per docs.python.org/3/library/stdtypes.html#string-methods. Much like alist, astris “iterable,” which means you can iterate over each of its characters in a loop. For instance, ifsis astr, you could print each of its characters, one at a time, with code like: for c in s: print(c, end="")
From the root of your repository execute cd 2-Loops So your current working directory is ...
/2-Loops $:Next execute
mkdir camelto make a folder called camel in your codespace.
Then execute
cd camelto change directories into that folder. You should now see your terminal prompt as /2-Loops/camel $. You can now execute
code camel.pyto make a file called camel.py where you’ll write your program.
Here’s how to test your code manually:
-
Run your program with
python camel.py. Typenameand press Enter. Your program should output:name
-
Run your program with
python camel.py. TypefirstNameand press Enter. Your program should output:first_name
-
Run your program with
python camel.py. TypepreferredFirstNameand press Enter. Your program should outputpreferred_first_name
At the /2-Loops/camel $ prompt in your terminal:
git add -A Add all changed files in the repository to be committed
git commit -m “Upload completed camel.py“Commit all changes in the REPO with the comment “Upload completed camel.py“ note: If the file is not complete, adjust the comment to describes what is being commited
git push Push all changes to the REPO
