Programming from zero

Build a health check URL

easyNumbers and strings

Problem statement

A monitoring script needs the full URL of a service's health endpoint. Given host = "api.internal" (a string) and port = 8080 (a number), print:

http://api.internal:8080/health

Examples

Example 1

Input: host = "api.internal" port = 8080

Output: http://api.internal:8080/health

Hints

Approach

Optimal

Python keeps numbers (int) and text (str) as separate types. You cannot join them with + directly: "http://" + 8080 fails with a TypeError. You have two options. You can convert the number with str(port) and then use +, or you can use an f-string, which converts every value inside {} to text automatically. The f-string version is shorter and easier to read, so it is the one used here.

Python
host = "api.internal"
port = 8080
url = f"http://{host}:{port}/health"
print(url)

Follow-up questions

  • Add a scheme variable so you can switch between http and https.

Frequently asked questions

Use type(), for example print(type(port)) prints <class 'int'>. This is useful when a value read from a file turns out to be text instead of a number.