Skip to content

Instantly share code, notes, and snippets.

@JTSGIT
Created November 15, 2023 16:12
Show Gist options
  • Save JTSGIT/3d786a6ef7fa0f524b340939ee47ec7c to your computer and use it in GitHub Desktop.
Save JTSGIT/3d786a6ef7fa0f524b340939ee47ec7c to your computer and use it in GitHub Desktop.
Kata5.py
def url_encode(text):
"""
This function receives a string of words and returns that string with all the whitespace characters
converted to %20, except for leading and trailing whitespace.
"""
# Trim leading and trailing whitespace
trimmed_text = text.strip()
# Encode spaces using a loop
encoded_text = ''
for char in trimmed_text:
if char == ' ':
encoded_text += '%20'
else:
encoded_text += char
return encoded_text
# Testing the function with different inputs
print(url_encode("Lighthouse Labs"))
print(url_encode(" Lighthouse Labs "))
print(url_encode("blue is greener than purple for sure"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment