{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "4489f00b", "metadata": { "id": "4489f00b" }, "source": [ "# Strings" ] }, { "cell_type": "markdown", "id": "e89aa34d", "metadata": { "id": "e89aa34d" }, "source": [ "## Comparar strings" ] }, { "cell_type": "code", "execution_count": 1, "id": "48b0180b", "metadata": { "id": "48b0180b", "outputId": "c699a559-d86f-4245-d0a7-a115449399c6", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 1 } ], "source": [ "cadena1 = \"Hola\" # con \"\"\n", "cadena2 = 'Hola' # con ''\n", "cadena1 == cadena2 # True" ] }, { "cell_type": "markdown", "id": "ffef0f9c", "metadata": { "id": "ffef0f9c" }, "source": [ "## Comillas dentro de comillas" ] }, { "cell_type": "code", "execution_count": 2, "id": "7199de29", "metadata": { "id": "7199de29" }, "outputs": [], "source": [ "#cadena3 = 'En los años 90's actuaban grandes bandas' # error" ] }, { "cell_type": "code", "execution_count": 3, "id": "d0b07aaa", "metadata": { "id": "d0b07aaa", "outputId": "ac2a6293-05db-447e-d2ab-9c279fa181cf", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "En los años 90's actuaban grandes bandas\n" ] } ], "source": [ "cadena3 = \"En los años 90's actuaban grandes bandas\" # ahora funciona bien usando comillas dobles\n", "print(cadena3)" ] }, { "cell_type": "code", "execution_count": 4, "id": "a1768d79", "metadata": { "id": "a1768d79", "outputId": "c345b641-2327-4801-9a6a-66affa08729a", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "En los años 90's actuaban grandes bandas\n" ] } ], "source": [ "print('En los años 90\\'s actuaban grandes bandas') # también funciona bien usando un carácter de escape" ] }, { "cell_type": "markdown", "id": "08b7ca74", "metadata": { "id": "08b7ca74" }, "source": [ "## Caracteres de escape" ] }, { "cell_type": "code", "execution_count": 5, "id": "c9f63a92", "metadata": { "id": "c9f63a92", "outputId": "5cae4d16-05c3-4891-d4c1-46e6cd66dc9f", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Secuencias de escape: \\ ' \" \n" ] } ], "source": [ "print('Secuencias de escape: \\\\ \\' \\\" ')" ] }, { "cell_type": "markdown", "id": "18dd8b12", "metadata": { "id": "18dd8b12" }, "source": [ "## Tabuladores" ] }, { "cell_type": "code", "execution_count": 6, "id": "ce53d6c3", "metadata": { "id": "ce53d6c3", "outputId": "4639bfaa-133e-418b-8423-99a5d7fc3d3f", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Mis amigos: \n", "Ana \tMadrid \n", "Luis \tSevilla \n", "Isabel \tSantander\n" ] } ], "source": [ "print('Mis amigos: \\nAna \\tMadrid \\nLuis \\tSevilla \\nIsabel \\tSantander')" ] }, { "cell_type": "code", "execution_count": 7, "id": "7fe9ea09", "metadata": { "id": "7fe9ea09", "outputId": "54d80b81-2193-4999-e8bb-4d69b27ba442", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Tareas pendientes:\n", "\tBuscar destino para las próximas vacaciones\n", "\tBuscar una bici eléctrica\n" ] } ], "source": [ "# En un párrafo usando comillas triples\n", "\n", "print('''Tareas pendientes:\n", "\\tBuscar destino para las próximas vacaciones\n", "\\tBuscar una bici eléctrica''')" ] }, { "cell_type": "markdown", "id": "9608df85", "metadata": { "id": "9608df85" }, "source": [ "## Imprimir una variable" ] }, { "cell_type": "code", "execution_count": 8, "id": "598ef39f", "metadata": { "id": "598ef39f", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "d312d7ae-aefb-4da8-b5ce-cc9d84f453f8" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Hola\n" ] } ], "source": [ "a = \"Hola\"\n", "print(a)" ] }, { "cell_type": "markdown", "id": "d2f245f7", "metadata": { "id": "d2f245f7" }, "source": [ "## Comillas triples\n", "Las triples comillas permiten imprimir varias líneas." ] }, { "cell_type": "code", "execution_count": 9, "id": "7d2e4516", "metadata": { "id": "7d2e4516", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "6eb6e56c-785f-410b-9d02-a494a1d3d900" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Esta es una frase que\n", "ocupa varias líneas y que va\n", "entre triples comillas que\n", "pueden ser simples o dobles.\n" ] } ], "source": [ "b = '''Esta es una frase que\n", "ocupa varias líneas y que va\n", "entre triples comillas que\n", "pueden ser simples o dobles.'''\n", "print(b)" ] }, { "cell_type": "markdown", "id": "e3357909", "metadata": { "id": "e3357909" }, "source": [ "## len()\n", "Proporciona la longitud de la cadena." ] }, { "cell_type": "code", "execution_count": 10, "id": "3e3c9ba9", "metadata": { "id": "3e3c9ba9", "outputId": "897aaae5-ea70-4f99-a620-aae8a5eed8c8", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "13" ] }, "metadata": {}, "execution_count": 10 } ], "source": [ "c = \"¡Hola, mundo!\"\n", "len(c)" ] }, { "cell_type": "markdown", "id": "6fa1b9d6", "metadata": { "id": "6fa1b9d6" }, "source": [ "## in\n", "Permite detectar si una palabra está dentro de una frase." ] }, { "cell_type": "code", "execution_count": 11, "id": "93ce0cc7", "metadata": { "id": "93ce0cc7", "outputId": "494ad53b-80b7-4e8c-dddf-167cc85458cd", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 11 } ], "source": [ "frase = \"La libertad, Sancho, es uno de los más preciosos dones que a los hombres dieron los cielos\"\n", "\"libertad\" in frase # True" ] }, { "cell_type": "markdown", "id": "df420b3b", "metadata": { "id": "df420b3b" }, "source": [ "## not in" ] }, { "cell_type": "code", "execution_count": 12, "id": "db0bfdf1", "metadata": { "id": "db0bfdf1", "outputId": "afd00df6-7ce6-4480-c48b-12670a12d9ac", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 12 } ], "source": [ "frase = \"Todos los seres humanos nacen libres e iguales en dignidad y derechos\"\n", "\"Sancho\" not in frase # True" ] }, { "cell_type": "markdown", "id": "04cc683b", "metadata": { "id": "04cc683b" }, "source": [ "## Orden alfabético" ] }, { "cell_type": "code", "execution_count": 13, "id": "4a486dc4", "metadata": { "id": "4a486dc4", "outputId": "5d8a1c8c-027c-4e0c-9b24-2612028bb66b", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 13 } ], "source": [ "\"b\" > \"a\" # True" ] }, { "cell_type": "code", "execution_count": 14, "id": "745dba35", "metadata": { "id": "745dba35", "outputId": "730c0141-3e9e-4f9c-bc65-73ec636c70d5", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {}, "execution_count": 14 } ], "source": [ "\"naciente\" > \"yaciente\" # False" ] }, { "cell_type": "markdown", "id": "17e073bf", "metadata": { "id": "17e073bf" }, "source": [ "## Concatenar" ] }, { "cell_type": "code", "execution_count": 15, "id": "629e9e70", "metadata": { "id": "629e9e70", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "c118d93f-64fc-48ed-850f-6459e96236ac" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'buenaaventura'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 15 } ], "source": [ "\"buena\" + \"aventura\"" ] }, { "cell_type": "code", "execution_count": 16, "id": "aa3862cb", "metadata": { "id": "aa3862cb", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "f4c68768-d5ec-443d-bd6e-b432459cdd73" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'enhorabuena'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 16 } ], "source": [ "'en' 'hora' 'buena' # también podemos separar los strings con un espacio" ] }, { "cell_type": "markdown", "id": "e7830ad5", "metadata": { "id": "e7830ad5" }, "source": [ "## replace()" ] }, { "cell_type": "code", "execution_count": 17, "id": "9b8aaa27", "metadata": { "id": "9b8aaa27", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "76c737d7-8f59-4d34-f6d5-368d1b7f44c9" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'mola'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 17 } ], "source": [ "\"hola\".replace('h','m')" ] }, { "cell_type": "code", "execution_count": 18, "id": "d2fe0f10", "metadata": { "id": "d2fe0f10", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "d1ca4a4a-2bb9-4300-d3cc-79e002f9ee91" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Voy a Niza'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 18 } ], "source": [ "\"Voy a Roma\".replace(\"Roma\",\"Niza\")" ] } ], "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": "0100_comparar_strings.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }