0Pricing
Flask Academy · Aula

Conversores de tipo: int, float, string, path

Restrinja segmentos de URL com conversores integrados.

Conversores de tipo: int, float, string, path é uma aula grátis de Flask Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Flask Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Flask Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

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 label

The 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 filepath

Pick 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. 🧭

Perguntas Frequentes

A aula “Conversores de tipo: int, float, string, path” é grátis?

Sim — o texto completo de “Conversores de tipo: int, float, string, path” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Flask Academy, atualize para CoddyKit PRO. O curso de Flask Academy inclui 4 aulas no total.

O que vou aprender em “Conversores de tipo: int, float, string, path”?

Restrinja segmentos de URL com conversores integrados. Você pratica Flask Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Flask Academy?

Nenhuma experiência prévia é necessária. Flask Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.

Quanto tempo leva a aula “Conversores de tipo: int, float, string, path”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Flask Academy?

Sim. Cada aula de Flask Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Capture variáveis do caminho
  2. Conversores de tipo: int, float, string, path
  3. Crie URLs com url_for
  4. Barras finais e comportamento de redirecionamento
← Voltar para Flask Academy