|
| 1 | +# Day 6 - String Formatting & F-Strings |
| 2 | +Python has ways to make writing text a bit more programmic and helps systematically remove repeating youself. |
| 3 | + |
| 4 | +Also, check out: |
| 5 | +- [pyformat](https://pyformat.info/) |
| 6 | +- [strftime.org](https://strftime.org/) |
| 7 | + |
| 8 | +### String Formatting |
| 9 | + |
| 10 | +Basic formatting: |
| 11 | + |
| 12 | +`#`: Write a comment `# this is a comment` |
| 13 | + |
| 14 | +`\n`: New Line |
| 15 | + |
| 16 | +`\t`: Tab |
| 17 | + |
| 18 | +`\\` or `//`: Allowed Slash |
| 19 | + |
| 20 | +`\'`: Allowed Single Quote |
| 21 | + |
| 22 | +`\"`: Allowed Double quote |
| 23 | + |
| 24 | +`{{` or `}}`: Allowed signle curly bracket in formatted strings |
| 25 | + |
| 26 | +`"your single-line text"`: Wrap a single quote (`'`) or double quote (`"`) around text / numbers to make it a string. |
| 27 | + |
| 28 | +`\`: A `slash` in front of a `return`/`enter` will escape that. Allowing for multi-line strings without the triple quotes. Such as: |
| 29 | +```python |
| 30 | +"this is my string example\ |
| 31 | +when I close it here" |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +`""" your multi-line text"""`: Wrap 3x single quotes (```) or 3x double quotes (`"`) around a lot of text to allow for multi-line strings. Such as: |
| 36 | +```python |
| 37 | +"""this is my string example |
| 38 | +when I close it here""" |
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +#### The `.format()` method |
| 44 | + |
| 45 | +_Empty_ |
| 46 | +```python |
| 47 | +"{} {}".format("Hello", "World") |
| 48 | +``` |
| 49 | + |
| 50 | +_Positional_ |
| 51 | +```python |
| 52 | +"{0} {1} {0}".format("Hello", "World") |
| 53 | +``` |
| 54 | + |
| 55 | +_Keyword_ |
| 56 | +```python |
| 57 | +"{first} {second} {first}".format(first="Hello", second="World") |
| 58 | +``` |
| 59 | + |
| 60 | +_Positional & Keyword_ |
| 61 | +```python |
| 62 | +"{0} {second} {0}".format("Hello", second="World") |
| 63 | + |
| 64 | +"{0} {1} {2['hello']}".format("Hello", "World", {'hello': 'sup'}) |
| 65 | +``` |
| 66 | + |
| 67 | +_Unpacking a Dictionary_ |
| 68 | +```python |
| 69 | +data = {'name': 'Hodor', 'email': 'holdthe@door.com'} |
| 70 | +txt = 'Name: {name}\nEmail: {email}'.format(**data) |
| 71 | +print(txt) |
| 72 | +``` |
| 73 | + |
| 74 | +_Numbers, Floats & Decimals_ |
| 75 | + |
| 76 | +Number / Integer |
| 77 | +```python |
| 78 | +"{:d}".format(32) |
| 79 | +``` |
| 80 | +or |
| 81 | +```python |
| 82 | +"{}".format(32) |
| 83 | +``` |
| 84 | + |
| 85 | +Float / Decimal |
| 86 | +```python |
| 87 | +"{:f}".format(32) |
| 88 | +``` |
| 89 | + |
| 90 | +```python |
| 91 | +pi = 3.14159265359 |
| 92 | +"{:f}".format(pi) |
| 93 | +``` |
| 94 | + |
| 95 | +Limit to `n` decimal places. Replace `4` below with the number of decimal places to round to. |
| 96 | +```python |
| 97 | +pi = 3.14159265359 |
| 98 | +"{:.4f}".format(pi) |
| 99 | +``` |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +#### The `%` method |
| 104 | + |
| 105 | +_Positional - Strings or Numbers_ |
| 106 | +```python |
| 107 | +"%s %s %s %s" % ("Hello", 12, 131.312, {'hello': 'sup'}) |
| 108 | +``` |
| 109 | + |
| 110 | +_Keyword / Dictionary_ |
| 111 | +```python |
| 112 | +"%(first)s %(second)s" % {"first":"Hello", "second":"World"} |
| 113 | +``` |
| 114 | + |
| 115 | +_Keywords_ (Also known as named placeholders) |
| 116 | +```python |
| 117 | +data = {'name': 'Hodor', 'email': 'holdthe@door.com'} |
| 118 | +txt = 'Name: %(name)s\nEmail: %(email)s' % data |
| 119 | +print(txt) |
| 120 | +``` |
| 121 | + |
| 122 | +_Numbers, Floats & Decimals_ |
| 123 | + |
| 124 | +Number |
| 125 | +```python |
| 126 | +"%d" % (32) |
| 127 | +``` |
| 128 | +or |
| 129 | +```python |
| 130 | +"%s" % (32) |
| 131 | +``` |
| 132 | + |
| 133 | +Float |
| 134 | +```python |
| 135 | +"%f" % (32) |
| 136 | +``` |
| 137 | + |
| 138 | +```python |
| 139 | +pi = 3.14159265359 |
| 140 | +"%f" % (pi) |
| 141 | +``` |
| 142 | + |
| 143 | +Limit to `n` decimal places. Replace `2` below with the number of decimal places to round to. |
| 144 | +```python |
| 145 | +pi = 3.14159265359 |
| 146 | +"%.2f" % (pi) |
| 147 | +``` |
| 148 | + |
| 149 | +#### `f` Strings (aka `f-string`) |
| 150 | + |
| 151 | +_Strings or Number variables_ |
| 152 | +```python |
| 153 | +first = "Hello" |
| 154 | +second = "World" |
| 155 | +third = 32.3122 |
| 156 | +fourth = "{:.2f}".format(third) |
| 157 | +f"{first} {second} {first.upper()} {third} {fourth}" |
| 158 | +``` |
| 159 | + |
| 160 | +_Dictionary_ |
| 161 | +```python |
| 162 | +data = {'name': 'Hodor', 'email': 'holdthe@door.com'} |
| 163 | +txt = f'Name: {data["name"]}\nEmail: {data["email"]}' |
| 164 | +print(txt) |
| 165 | +``` |
| 166 | + |
| 167 | +_Inline Math_ |
| 168 | +```python |
| 169 | +hours = 21 |
| 170 | +seconds = 32 |
| 171 | +f"{hours} {seconds * 10} {seconds}" |
| 172 | +``` |
| 173 | + |
| 174 | +_Inline Formatting_ |
| 175 | + |
| 176 | + |
| 177 | +```python |
| 178 | +pi = 3.14159265359 |
| 179 | +f"{format(pi, '.2f')}" |
| 180 | +``` |
0 commit comments