{ "cells": [ { "cell_type": "markdown", "id": "5d6739ee-d653-43c9-b3a5-b7d1ff624e7e", "metadata": { "tags": [] }, "source": [ "Template Strings\n", "===\n", "\n", "* Duración de la lección: 3:47 min" ] }, { "cell_type": "code", "execution_count": 1, "id": "33875502-5eb3-46ab-8f43-ae0cd5edf021", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'-----> Texto1 Texto2 <------'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from string import Template\n", "\n", "some_template = Template(\"-----> $variable1 $variable2 <------\")\n", "some_template.substitute(variable1=\"Texto1\", variable2=\"Texto2\")" ] }, { "cell_type": "code", "execution_count": 2, "id": "86f6be4e-5def-434f-84ed-3792fc4cd4e6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'aaaAAAAaaa'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "other_template = Template(\"aaa${variable}aaa\")\n", "other_template.substitute(variable=\"AAAA\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "e89de752-d136-479d-b189-6acacdc72a4a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Juan gano $1000'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Uso con diccionarios\n", "#\n", "some_dict={'quien': 'Juan'}\n", "\n", "Template(\"$quien gano $$1000\").substitute(some_dict)" ] }, { "cell_type": "code", "execution_count": 4, "id": "b530d68e-6797-48d3-82fa-2965a5d75231", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Juan gano $1000, pero $otro no gano'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Template(\"$quien gano $$1000, pero $otro no gano\").safe_substitute(some_dict)" ] } ], "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.9" } }, "nbformat": 4, "nbformat_minor": 5 }