{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Los string se denotan con simples o dobles comillas " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hola mundo'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 'hola mundo'\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Podemos acceder por posicion a cada caracter" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'o'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Concatenarlos" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hola mundo/hola mundo'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = a + '/' + a\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Usar el autoincremento" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hola mundo/hola mundo mundo'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b += ' mundo'\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Y otras operaciones sobre cadenas habituales sobre strings" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['hola', 'mundo/hola', 'mundo', 'mundo']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.split(' ')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.find('mundo')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Replace retorna un nuevo string (son objetos inmutables)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hola mon/hola mon mon\n", "hola mundo/hola mundo mundo\n" ] } ], "source": [ "print(b.replace('mundo', 'mon'))\n", "print(b)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Templates" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'aa 3 bbb a ccc 2.30'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"aa %d bbb %s ccc %3.2f\" % (3, 'a', 2.3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Templates con la nueva notacion de python 3.6" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'aa 3 bbb a ccc 2.30'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 3\n", "b = 'a'\n", "c = 2.3\n", "\n", "f\"aa {a} bbb {b} ccc {c:3.2f}\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Operaciones sobre strings en https://docs.python.org/3/library/string.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.6" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "toc_cell": false, "toc_position": {}, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 1 }