|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Print multiple objects\n", |
| 8 | + "\n", |
| 9 | + "This exercise has two parts. \n", |
| 10 | + "\n", |
| 11 | + "1) Given three strings stored as variables, print the strings separated by a space." |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": null, |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "# Run this cell to create string objects\n", |
| 21 | + "string_1 = 'The first string'\n", |
| 22 | + "string_2 = 'and the second'\n", |
| 23 | + "string_3 = 'and the third.'" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "The `print()` function can take multiple input objects, and the default separator between the output is a space, so we can pass each object to `print()`." |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": null, |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [], |
| 38 | + "source": [ |
| 39 | + "print(string_1, string_2, string_3)" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "markdown", |
| 44 | + "metadata": {}, |
| 45 | + "source": [ |
| 46 | + "2) Print the strings separated by commas." |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "We can modify how the values of these objects in the output are separated by modifying the `sep` attribute." |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": null, |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [], |
| 61 | + "source": [ |
| 62 | + "print(string_1, string_2, string_3, sep=', ')" |
| 63 | + ] |
| 64 | + } |
| 65 | + ], |
| 66 | + "metadata": { |
| 67 | + "interpreter": { |
| 68 | + "hash": "397704579725e15f5c7cb49fe5f0341eb7531c82d19f2c29d197e8b64ab5776b" |
| 69 | + }, |
| 70 | + "kernelspec": { |
| 71 | + "display_name": "Python 3.9.7 64-bit", |
| 72 | + "language": "python", |
| 73 | + "name": "python3" |
| 74 | + }, |
| 75 | + "language_info": { |
| 76 | + "codemirror_mode": { |
| 77 | + "name": "ipython", |
| 78 | + "version": 3 |
| 79 | + }, |
| 80 | + "file_extension": ".py", |
| 81 | + "mimetype": "text/x-python", |
| 82 | + "name": "python", |
| 83 | + "nbconvert_exporter": "python", |
| 84 | + "pygments_lexer": "ipython3", |
| 85 | + "version": "3.9.7" |
| 86 | + }, |
| 87 | + "orig_nbformat": 4 |
| 88 | + }, |
| 89 | + "nbformat": 4, |
| 90 | + "nbformat_minor": 2 |
| 91 | +} |
0 commit comments