{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "b400c40e", "metadata": { "id": "b400c40e" }, "source": [ "# String como iterable\n", "Los índices comienzan por **cero**." ] }, { "cell_type": "markdown", "id": "7e2bfb40", "metadata": { "id": "7e2bfb40" }, "source": [ "![Buenos Aires](https://github.com/financieras/pyCourse/blob/main/jupyter/img/Buenos_Aires.png?raw=1)" ] }, { "cell_type": "code", "execution_count": 1, "id": "3671daf4", "metadata": { "id": "3671daf4", "outputId": "c160e61d-2071-4624-9569-760b5bf787f0", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "B\n", "u\n", "e\n", "n\n", "o\n", "s\n", " \n", "A\n", "i\n", "r\n", "e\n", "s\n" ] } ], "source": [ "ciudad = \"Buenos Aires\"\n", "print(ciudad[0]) # B se comienza por el índice 0\n", "print(ciudad[1]) # u\n", "print(ciudad[2]) # e\n", "print(ciudad[3]) # n\n", "print(ciudad[4]) # o\n", "print(ciudad[5]) # s\n", "print(ciudad[6]) #\n", "print(ciudad[7]) # A\n", "print(ciudad[8]) # i\n", "print(ciudad[9]) # r\n", "print(ciudad[10]) # e\n", "print(ciudad[11]) # s" ] }, { "cell_type": "markdown", "id": "c47ea911", "metadata": { "id": "c47ea911" }, "source": [ "## Índices negativos" ] }, { "cell_type": "code", "execution_count": 2, "id": "19152c02", "metadata": { "id": "19152c02", "outputId": "119854c6-8510-43a9-9f5c-7ae94db7633b", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "12" ] }, "metadata": {}, "execution_count": 2 } ], "source": [ "len(ciudad)" ] }, { "cell_type": "code", "execution_count": 3, "id": "745534ab", "metadata": { "id": "745534ab", "outputId": "d557d1f0-67f4-4b06-f4ef-40d3c2a0a1bc", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "s\n", "e\n", "r\n", "i\n", "A\n", " \n", "s\n", "o\n", "n\n", "e\n", "u\n", "B\n" ] } ], "source": [ "print(ciudad[-1]) # s\n", "print(ciudad[-2]) # e\n", "print(ciudad[-3]) # r\n", "print(ciudad[-4]) # i\n", "print(ciudad[-5]) # A\n", "print(ciudad[-6]) #\n", "print(ciudad[-7]) # s\n", "print(ciudad[-8]) # o\n", "print(ciudad[-9]) # n\n", "print(ciudad[-10]) # e\n", "print(ciudad[-11]) # u\n", "print(ciudad[-12]) # B" ] }, { "cell_type": "code", "execution_count": 4, "id": "c64f4f2b", "metadata": { "id": "c64f4f2b", "outputId": "74a42699-3dca-4725-a1dc-414e6bab95e4", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "s\n", "B\n" ] } ], "source": [ "print(ciudad[len(ciudad)-1]) # s\n", "print(ciudad[-len(ciudad)]) # B" ] }, { "cell_type": "markdown", "id": "1f3baff5", "metadata": { "id": "1f3baff5" }, "source": [ "## Slicing (métodos de corte)" ] }, { "cell_type": "code", "execution_count": 5, "id": "5a4aa67a", "metadata": { "id": "5a4aa67a", "outputId": "a22d1729-f29e-4752-8bf8-00e4a028d049", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Buen\n", "Buen\n", "Air\n", "Buenos Aires\n", "eo i\n", "Aires\n" ] } ], "source": [ "print(ciudad[0:4]) # Buen\n", "print(ciudad[:4]) # Buen\n", "print(ciudad[7:10]) # Air\n", "print(ciudad[:]) # Buenos Aires\n", "print(ciudad[2:9:2]) # eo i\n", "print(ciudad[-5:]) # Aires" ] }, { "cell_type": "code", "execution_count": 6, "id": "8b30a834", "metadata": { "scrolled": true, "id": "8b30a834" }, "outputs": [], "source": [ "#ciudad[30] # IndexError: string index out of range" ] }, { "cell_type": "code", "execution_count": 7, "id": "662735f2", "metadata": { "id": "662735f2", "outputId": "e4fbb47a-d222-4511-b327-b3d723d713d4", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Aires'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 7 } ], "source": [ "ciudad[7:30] # Con slicing si funciona aunque nos salgamos del índice" ] }, { "cell_type": "code", "execution_count": 8, "id": "98486507", "metadata": { "id": "98486507", "outputId": "9fb714e8-8494-45b4-d4bd-5eed156d4b9a", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "''" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 8 } ], "source": [ "ciudad[30:] # aquí no hay nada. No da error, devuelve cadena vacía" ] }, { "cell_type": "markdown", "id": "b53f8296", "metadata": { "id": "b53f8296" }, "source": [ "## Reverso de un string" ] }, { "cell_type": "code", "execution_count": 9, "id": "02754411", "metadata": { "id": "02754411", "outputId": "9ce41f94-cbd8-4515-fa00-1b06d8e174c8", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "amigos\n" ] } ], "source": [ "palabra = 'sogima'\n", "print(palabra[::-1]) # reverso de una palabra" ] }, { "cell_type": "markdown", "id": "6ebd9f36", "metadata": { "id": "6ebd9f36" }, "source": [ "## Cadena completa" ] }, { "cell_type": "code", "execution_count": 10, "id": "f8986d61", "metadata": { "id": "f8986d61", "outputId": "ae36e454-44b8-4d04-d1ff-334097ce98a9", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Buenos Aires'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 10 } ], "source": [ "ciudad[:]" ] }, { "cell_type": "code", "execution_count": 11, "id": "e77c08ec", "metadata": { "id": "e77c08ec", "outputId": "77dd2e1d-a3fa-4f86-e99f-b50f78af2d72", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Buenos Aires'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 11 } ], "source": [ "ciudad[:5] + ciudad[5:]" ] }, { "cell_type": "markdown", "id": "2a42e01c", "metadata": { "id": "2a42e01c" }, "source": [ "## Las cadenas son inmutables\n", "Si intentamos cambiar un carácter nos dará error." ] }, { "cell_type": "code", "execution_count": 12, "id": "63de3245", "metadata": { "id": "63de3245" }, "outputs": [], "source": [ "#ciudad[0] = 'G' # TypeError: 'str' object does not support item assignment" ] }, { "cell_type": "markdown", "id": "30d567f1", "metadata": { "id": "30d567f1" }, "source": [ "Esto no es un gran inconveniente ya que podemos conseguirlo con slicing." ] }, { "cell_type": "code", "execution_count": 13, "id": "b7ee4486", "metadata": { "id": "b7ee4486", "outputId": "10033ab8-c873-4ab2-8b14-dacd1f1f75bf", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Guenos Aires'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 13 } ], "source": [ "ciudad=\"G\" + ciudad[1:]\n", "ciudad" ] } ], "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": "0140_string_como_iterable.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }