curriculum/challenges/english/blocks/daily-coding-challenges-python/68b06e589bf2273243814775.md
Given a string, return a URL-friendly version of the string using the following constraints:
%20.%20.%20.generate_slug("helloWorld") should return "helloworld".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(generate_slug("helloWorld"), "helloworld")`)
}})
generate_slug("hello world!") should return "hello%20world".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(generate_slug("hello world!"), "hello%20world")`)
}})
generate_slug(" hello-world ") should return "helloworld".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(generate_slug(" hello-world "), "helloworld")`)
}})
generate_slug("hello world") should return "hello%20world".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(generate_slug("hello world"), "hello%20world")`)
}})
generate_slug(" ?H^3-1*1]0! W[0%R#1]D ") should return "h3110%20w0r1d".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(generate_slug(" ?H^3-1*1]0! W[0%R#1]D "), "h3110%20w0r1d")`)
}})
def generate_slug(str):
return str
import re
def generate_slug(s):
cleaned = re.sub(r'[^a-zA-Z0-9 ]+', '', s)
cleaned = re.sub(r'\s+', ' ', cleaned).strip()
return cleaned.lower().replace(' ', '%20')