Files
teaching/python-ws2425/12. String Formatting.nbconvert.ipynb
2024-09-19 08:27:13 +00:00

326 lines
6.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "5ec829a7",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# String formatting"
]
},
{
"cell_type": "markdown",
"id": "130cc097",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"We have talked about strings before, and you know that it is possible to construct strings containing values, e.g."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "51d2701f",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"'x=' + str(43.2) + ', y=' + str(1./3.)"
]
},
{
"cell_type": "markdown",
"id": "bf0f4231",
"metadata": {},
"source": [
"However, one may want to format the values in more detail, for example forcing values to be a certain length, or have a certain number of decimal places. This is called *string formatting*."
]
},
{
"cell_type": "markdown",
"id": "8a6a1b0e",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"The syntax for formatting strings looks like this:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "462c1d03",
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"\"{0} {1} {2}\".format(1./3., 2./7., 2.)"
]
},
{
"cell_type": "markdown",
"id": "e2522672",
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"source": [
"In the above example, the ``0``, ``1``, and ``2`` refer to the position of the argument in the parentheses, so one could also do:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "131d89ab",
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"\"The quantity {0} that could also be called {0} {1}\".format(1./3., 2./7.)"
]
},
{
"cell_type": "markdown",
"id": "8b98fc21",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"By default, the value looks the same as if one had used ``str()``, but you can also specify the format and number of decimal places:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c417f3b5",
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"\"{0:10.3f}\".format(1./3.)"
]
},
{
"cell_type": "markdown",
"id": "c5257ae2",
"metadata": {},
"source": [
"The ``f`` stands for floating-point, the 10 is the total length of the string, and the 3 is the nuber of decimal places."
]
},
{
"cell_type": "markdown",
"id": "a9fe4cd2",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"We can do something similar for integers (without the number of decimal places):"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3c7f537f",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\"{0:10d}\".format(4)"
]
},
{
"cell_type": "markdown",
"id": "787175da",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"There are a number of options for string formatting - for instance, one can add leading zeros:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "14c4d480",
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"\"{0:010d}\".format(4)"
]
},
{
"cell_type": "markdown",
"id": "f3895f73",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"or align to the left:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "63d1da2d",
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"\"{0:<10d}\".format(4)"
]
},
{
"cell_type": "markdown",
"id": "c0ca021a",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Instead of using ``0``, ``1``, etc. it is also possible to pass the values by name:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8867ffb4",
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"\"{day:02d}\".format(day=3)"
]
},
{
"cell_type": "markdown",
"id": "84803075",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Here is an example of string formatting for a date:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dcf4ee9c",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\"{year:04d}{month:02d}{day:02d}\".format(year=2013, month=7, day=8)"
]
},
{
"cell_type": "markdown",
"id": "99bf270d",
"metadata": {},
"source": [
"## Exercise 1"
]
},
{
"cell_type": "markdown",
"id": "c05ba888",
"metadata": {},
"source": [
"Write a function that takes year, month, day, hour, minutes, and seconds and converts them to a string with format ``2006-03-22 13:12:55``:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67999f6a",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# your solution here"
]
},
{
"cell_type": "markdown",
"id": "1a7cfc85",
"metadata": {},
"source": [
"## Exercise 2"
]
},
{
"cell_type": "markdown",
"id": "c80325a6",
"metadata": {},
"source": [
"Write a function that takes a string like ``2006-03-22 13:12:55`` and return the year, month, day, hour, minutes, and seconds as integers:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "77fc6ff6",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# your solution here"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}