{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "789833ee", "metadata": { "id": "789833ee" }, "source": [ "# Formateando Cadenas (Strings)\n", "\n", "\n", "## Sistema antiguo con ```%s```" ] }, { "cell_type": "code", "execution_count": 1, "id": "c0387e81", "metadata": { "id": "c0387e81", "outputId": "958e9969-ae87-43d3-bbcc-e709489222a3", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "La capital de Lituania es Vilna\n" ] } ], "source": [ "print(\"La capital de %s es %s\"%(\"Lituania\", \"Vilna\"))" ] }, { "cell_type": "markdown", "id": "40007ba8", "metadata": { "id": "40007ba8" }, "source": [ "## Usando ```format```" ] }, { "cell_type": "code", "execution_count": 2, "id": "c31fb145", "metadata": { "id": "c31fb145", "outputId": "d221ed9f-46f4-4ebd-db17-35af6c29f120", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Cuando tenía 10 años media 140 cm.\n" ] } ], "source": [ "edad = 10\n", "print('Cuando tenía {} años media {} cm.'.format(edad, 140))" ] }, { "cell_type": "code", "execution_count": 3, "id": "85018905", "metadata": { "id": "85018905", "outputId": "f2573a44-0076-4264-d6de-47a61ec8020f", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "La suma de 2 más 3 es igual a 5\n" ] } ], "source": [ "print(\"La suma de {} más {} es igual a {}\".format(2, 3, 2+3))" ] }, { "cell_type": "code", "execution_count": 4, "id": "35918965", "metadata": { "id": "35918965", "outputId": "9fe64c06-78ba-4754-d803-caf0bfe38751", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "El producto de 5 y 4 es 20\n" ] } ], "source": [ "texto = \"El producto de {} y {} es {}\"\n", "print(texto.format(5, 4, 20))" ] }, { "cell_type": "code", "execution_count": 5, "id": "b23ef7f3", "metadata": { "id": "b23ef7f3", "outputId": "555d3ad8-6558-4cda-bcd2-50f03a99f8c3", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Me llamo Luis Olmo\n" ] } ], "source": [ "nombre = \"Luis\"\n", "apellido = \"Olmo\"\n", "print(\"Me llamo {} {}\".format(nombre, apellido))" ] }, { "cell_type": "code", "execution_count": 6, "id": "ef7f6b0a", "metadata": { "id": "ef7f6b0a", "outputId": "5df336d2-bd12-449e-bbcf-00567f33b08a", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Me llamo Ana Vega pero llámame simplemente Ana\n" ] } ], "source": [ "nombre = \"Ana\"\n", "apellido = \"Vega\"\n", "texto = \"Me llamo {0} {1} pero llámame simplemente {0}\"\n", "print(texto.format(nombre, apellido)) # el primero es el de índice 0" ] }, { "cell_type": "code", "execution_count": 7, "id": "a11eb886", "metadata": { "id": "a11eb886", "outputId": "51134bdb-7969-4e50-c3fe-b53fe8bff214", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "El Sol y la Luna\n" ] } ], "source": [ "texto = \"El {b} y la {a}\"\n", "print(texto.format(a = \"Luna\", b = \"Sol\"))" ] }, { "cell_type": "code", "execution_count": 8, "id": "040ebe18", "metadata": { "id": "040ebe18", "outputId": "7a5e8be6-1c8f-4161-8174-b5430062dc8d", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Nuestro amigo Juan cumple 24 años\n" ] } ], "source": [ "nombre = \"Juan\"\n", "edad = 24\n", "print(\"Nuestro amigo {} cumple {} años\".format(nombre,edad))" ] }, { "cell_type": "code", "execution_count": 9, "id": "b9ab5b87", "metadata": { "id": "b9ab5b87", "outputId": "53785f2f-9711-416b-b849-a83d7c4f8408", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Para conseguir tu objetivo necesitas tres cosas: motivación, motivación y motivación.\n" ] } ], "source": [ "print(\"Para conseguir tu objetivo necesitas tres cosas: {m}, {m} y {m}.\".format(m='motivación'))" ] }, { "cell_type": "markdown", "id": "395c2c29", "metadata": { "id": "395c2c29" }, "source": [ "## El sistema ```f```" ] }, { "cell_type": "code", "execution_count": 10, "id": "49c26ddc", "metadata": { "id": "49c26ddc", "outputId": "f98c42d1-7300-4a42-f1b9-b28644cc413f", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Nuestro amigo Juan cumple 24 años\n" ] } ], "source": [ "nombre = \"Juan\"\n", "edad = 24\n", "print(f\"Nuestro amigo {nombre} cumple {edad} años\")" ] }, { "cell_type": "code", "execution_count": 11, "id": "8bce4ed7", "metadata": { "id": "8bce4ed7", "outputId": "2e8bdf14-aac4-4d7a-fac3-927216b484ee", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "La suma de 4 más 6 es igual a 10\n" ] } ], "source": [ "x = 4\n", "y = 6\n", "z = x + y\n", "print(f\"La suma de {x} más {y} es igual a {z}\")" ] }, { "cell_type": "markdown", "id": "f7755624", "metadata": { "id": "f7755624" }, "source": [ "Podemos realizar operaciones en las llaves, dentro de las comillas." ] }, { "cell_type": "code", "execution_count": 12, "id": "b27687d8", "metadata": { "id": "b27687d8", "outputId": "02d6c1fe-5aed-490c-fdac-fbb9045831cb", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "El área es 8\n" ] } ], "source": [ "print(f'El área es {2*4}')" ] }, { "cell_type": "markdown", "id": "4c95b292", "metadata": { "id": "4c95b292" }, "source": [ "### Invocando funciones\n", "Incluso podemos invocar funciones dentro de las comillas." ] }, { "cell_type": "code", "execution_count": 13, "id": "7c177f26", "metadata": { "id": "7c177f26", "outputId": "23466754-8e00-484c-cbab-948674ef0c64", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "El doble de 9 es 18\n" ] } ], "source": [ "def doblar(n):\n", " return 2*n\n", "\n", "print(f\"El doble de 9 es {doblar(9)}\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.11" }, "colab": { "name": "0050_formateando_cadenas.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }