Compose API URLs Without Bugs

Easy ⏱ 8 min 82% acceptance ★★★★☆ 4.4
Naive f-strings produce https://api.x.com//v1/users or miss slashes. Write api_url(base, *segments) joining a base URL and path segments with exactly one / between parts, regardless of stray slashes on the inputs.

Examples

Example 1
Input
api_url('https://api.x.com/', '/v1/', 'users', 42)
Output
'https://api.x.com/v1/users/42'
Explanation

Extra slashes were normalized away.

Constraints

  • Accept non-string segments (str() them).
  • No doubled slashes in the result.

Topics

APIsurl building

Companies

PostmanTwilioInfosys

Hints

Hint 1

base.rstrip('/') plus each segment str(s).strip('/').

Hint 2

'/'.join finishes it.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.