{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "e98a92a5", "metadata": { "id": "e98a92a5" }, "source": [ "# Cadenas (Strings)" ] }, { "cell_type": "code", "execution_count": 1, "id": "4ee2203b", "metadata": { "id": "4ee2203b", "outputId": "0608f50b-b2c4-4f3f-b956-1a546ca1f54c", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'hola'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 1 } ], "source": [ "\"hola\" # directamente en el intérprete de Python" ] }, { "cell_type": "code", "execution_count": 2, "id": "e23a6dd7", "metadata": { "id": "e23a6dd7", "outputId": "09de4376-5ac0-4754-e6f0-e29a2d6bde0f", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Hola\n" ] } ], "source": [ "print(\"Hola\") # usando la función print" ] }, { "cell_type": "markdown", "id": "6081021a", "metadata": { "id": "6081021a" }, "source": [ "## Repetir expresiones" ] }, { "cell_type": "code", "execution_count": 3, "id": "31f0bcb0", "metadata": { "id": "31f0bcb0", "outputId": "08135181-c1da-4f59-db6f-e5ac3e76d766", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'hihihi'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 3 } ], "source": [ "\"hi\" * 3 # repetir expresiones" ] }, { "cell_type": "code", "execution_count": 4, "id": "3e6f7b72", "metadata": { "id": "3e6f7b72", "outputId": "7c3b8813-be29-468e-b1a2-bcb117bf744e", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Bienvenidos\n", "==============================\n", "Fin del progama\n" ] } ], "source": [ "print(\"Bienvenidos\")\n", "print(\"=\"*30)\n", "print(\"Fin del progama\")" ] }, { "cell_type": "markdown", "id": "58c650b0", "metadata": { "id": "58c650b0" }, "source": [ "## Concatenar expresiones" ] }, { "cell_type": "code", "execution_count": 5, "id": "e379b647", "metadata": { "id": "e379b647", "outputId": "c59b4b94-7bf7-41a6-cdd0-5fb01566e619", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'holamundo'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 5 } ], "source": [ "\"hola\" + \"mundo\" # concatenar" ] }, { "cell_type": "code", "execution_count": 6, "id": "1eb00665", "metadata": { "id": "1eb00665", "outputId": "445c3695-659c-4376-9752-9121b200c397", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "hola mundo\n" ] } ], "source": [ "print(\"hola\" + \" \" + \"mundo\")" ] }, { "cell_type": "markdown", "id": "5f9bfa3b", "metadata": { "id": "5f9bfa3b" }, "source": [ "## Asignando variables" ] }, { "cell_type": "code", "execution_count": 7, "id": "81059451", "metadata": { "id": "81059451", "outputId": "85d4aeba-6ddd-4ed5-a6d5-bbfbfb839fb5", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "holamundo\n" ] } ], "source": [ "string1 = \"hola\"\n", "string2 = \"mundo\"\n", "string3 = string1 + string2 # concatenando\n", "print(string3) # si olvidamos cerrar el paréntesis se producirá un Error de sintaxis" ] }, { "cell_type": "markdown", "id": "01950c38", "metadata": { "id": "01950c38" }, "source": [ "Un enlace a un post que muestra múltiples formas de decir 'Hola mundo' en Python.\n", "\n", "[Hola mundo en Python](https://altocodigo.blogspot.com/2018/06/1-hola-mundo-en-python.html)" ] }, { "cell_type": "markdown", "id": "11020dc7", "metadata": { "id": "11020dc7" }, "source": [ "## Error al concatenar con un número" ] }, { "cell_type": "code", "execution_count": 8, "id": "5f4a6df6", "metadata": { "id": "5f4a6df6" }, "outputs": [], "source": [ "#\"Vuelo \" + 505 # TypeError: can only concatenate str (not \"int\") to str" ] }, { "cell_type": "code", "execution_count": 9, "id": "86e3504e", "metadata": { "id": "86e3504e", "outputId": "d9eeb372-360b-43da-a43c-ac875e55cd04", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Vuelo 505'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 9 } ], "source": [ "\"Vuelo \" + str(505) # la función str() convierte un número en cadena" ] }, { "cell_type": "markdown", "source": [ "### Mi nombre y mi edad\n", "A la edad la tenemos que añadir la función `str()` que convierte un número en cadena (string)." ], "metadata": { "id": "A4n4DBvZK02x" }, "id": "A4n4DBvZK02x" }, { "cell_type": "code", "source": [ "nombre = \"Alex\"\n", "edad = 30\n", "print(\"Me llamo \" + nombre + \" y tengo \" + str(edad) + \" años.\")" ], "metadata": { "id": "MfL3KJ6mLOjz", "outputId": "c871a45b-cc84-4d9c-9989-6f47961d74f7", "colab": { "base_uri": "https://localhost:8080/" } }, "id": "MfL3KJ6mLOjz", "execution_count": 10, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Me llamo Alex y tengo 30 años.\n" ] } ] }, { "cell_type": "markdown", "source": [ "#### Otra forma de escribirlo más moderna, con formato" ], "metadata": { "id": "OTx36JOWLlrt" }, "id": "OTx36JOWLlrt" }, { "cell_type": "code", "source": [ "print(f\"Me llamo {nombre} y tengo {edad} años.\")" ], "metadata": { "id": "djkwBfKBLr0F", "outputId": "f1c07e62-5ed1-46d2-b3c2-e376449b05b6", "colab": { "base_uri": "https://localhost:8080/" } }, "id": "djkwBfKBLr0F", "execution_count": 11, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Me llamo Alex y tengo 30 años.\n" ] } ] }, { "cell_type": "markdown", "id": "d2d77353", "metadata": { "id": "d2d77353" }, "source": [ "## Comparando\n", "* =     se usa para asignar un valor a una variable\n", "* ==   se usa para comparar dos valores o variables, formándose una expresión lógica" ] }, { "cell_type": "code", "execution_count": 12, "id": "1d3a196b", "metadata": { "id": "1d3a196b", "outputId": "0dd8dd5d-9cbb-40fe-e2ca-127267958b8a", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 12 } ], "source": [ "2 + 8 == 10 #True" ] }, { "cell_type": "code", "execution_count": 13, "id": "1b908195", "metadata": { "id": "1b908195", "outputId": "ea5bc846-113f-44a1-a9ae-9a32e12f656c", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {}, "execution_count": 13 } ], "source": [ "123 == '123' #False" ] }, { "cell_type": "code", "execution_count": 14, "id": "bce9b6a9", "metadata": { "id": "bce9b6a9", "outputId": "377da951-bb09-49f2-b5eb-ea8af2057a10", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 14 } ], "source": [ "'Hola' == \"Hola\" #True" ] }, { "cell_type": "code", "execution_count": 15, "id": "e25dd0e9", "metadata": { "id": "e25dd0e9", "outputId": "292e6f47-4b24-4ece-e66e-264b1da22f9b", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 15 } ], "source": [ "\"A\" + \"B\" == \"AB\" #True" ] } ], "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": "0040_strings.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }