0Pricing
Flask Academy · Lesson

Type Converters: int, float, string, path

Constrain URL segments with built-in converters.

Strings Are Not Always Enough

Captured values default to strings, but /post/42 should give a number, not text. A converter tells Flask what type to expect in that segment.

Converter Syntax

Put the converter name before the variable with a colon. The pattern is <converter:name>, and Flask applies it before calling your view.

@app.route('/post/<int:post_id>')
def show(post_id):
    return str(post_id + 1)

All lessons in this course

  1. Capture Variables from the Path
  2. Type Converters: int, float, string, path
  3. Build URLs with url_for
  4. Trailing Slashes and Redirect Behavior
← Back to Flask Academy