Python code and Git/GitHub steps can be implemented as follows.
- Create the initial Python function, commit it, and push
Example reverse.py:
def reverse_string(s: str) -> str:
return s[::-1]
if __name__ == "__main__":
text = input("Enter text: ")
print(reverse_string(text))
From the repo folder:
# Stage and commit initial version
git add reverse.py
git commit -m "Add basic reverse_string function"
# Push current branch (for example, main) to origin
git push origin main
git add stages the file, git commit -m saves it to the current branch, and git push origin main sends the commit to the remote repository.
- Create and switch to the
update branch
Create a new branch for the update work and switch to it:
# Create and switch in one step
git checkout -b update
This follows the recommended workflow of creating a new branch for each feature or fix.
- Modify the function to handle empty input
Update reverse.py, for example:
def reverse_string(s: str | None) -> str:
"""Reverse a string. Treat None or empty input as an empty string."""
if not s:
return ""
return s[::-1]
if __name__ == "__main__":
text = input("Enter text (leave empty for default): ")
print(reverse_string(text))
- Commit the changes on the
update branch
# Stage and commit the modification
git add reverse.py
git commit -m "Handle empty and None input in reverse_string"
git add . could be used instead to stage all modified files in the repo.
- Push the
update branch to GitHub
# Push the new branch to the remote
git push origin update
This shares the update branch and its commits with the remote repository so it can be reviewed or merged via a pull request.
References: