Type Converters: int, float, string, path
Constrain URL segments with built-in converters.
Type Converters: int, float, string, path is a free Flask Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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)The int Converter
Use int to capture whole numbers. /post/42 hands your view the integer 42, so you can do math without calling int() yourself. 🔢
@app.route('/page/<int:n>')
def page(n):
return f'Page {n}'int Rejects Non-Numbers
A converter also filters URLs. /page/abc will not match an int route, so Flask returns 404 instead of crashing inside your function.
The float Converter
Use float for decimal numbers like prices or ratings. /rate/4.5 gives you the float 4.5, ready for arithmetic right away.
@app.route('/rate/<float:score>')
def rate(score):
return str(score * 2)The string Converter
string is the default: it accepts any text but stops at a slash. You rarely write it explicitly since plain <name> already behaves this way.
@app.route('/tag/<string:label>')
def tag(label):
return labelThe path Converter
Use path when the value can contain slashes, like a folder route. Unlike string, it captures /docs/intro/setup as one whole value.
@app.route('/files/<path:filepath>')
def serve(filepath):
return filepathPick the Right Tool
Match the converter to your data: int for ids, float for decimals, string for words, path for slashed routes. The right choice prevents bad input early.
Converters Validate for You
By rejecting mismatched URLs with a 404, converters act as a first line of validation. Your view only runs when the type already fits.
Combine in One Route
You can mix converters across segments. Here an int id and a string tab live in the same path, each typed independently.
@app.route('/user/<int:uid>/<tab>')
def view(uid, tab):
return f'{uid}:{tab}'Negatives and Edge Cases
The built-in int converter matches only non-negative whole numbers by default. For minus signs or stricter rules, you would write a custom converter later.
Quick Check
Which converter lets a captured value include slashes, like docs/intro/setup?
Recap: You Typed Your URLs
Converters give captured values a type and reject bad input with a 404. Remember int, float, string, and path. Next: building URLs safely. 🧭
Frequently asked questions
Is the “Type Converters: int, float, string, path” lesson free?
Yes — the full text of “Type Converters: int, float, string, path” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Type Converters: int, float, string, path”?
Constrain URL segments with built-in converters. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Type Converters: int, float, string, path” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Capture Variables from the Path
- Type Converters: int, float, string, path
- Build URLs with url_for
- Trailing Slashes and Redirect Behavior