<jupyter_start><jupyter_text>A shocking truth about string formatting '*Old Style*' String formattingStrings in Python have a unique built-in operation that can be accessed with the %-operator. It's a shortcut that lets you do simple positional formatting very easily.In old style string formatting there are also other format specifiers available that let you control string. For example, It's possible to convert numbers to hexadecimal notation.It's also possible to refer to variable substitutions by name in your format string, if you pass a mapping to the %-operator.This makes your format strings easier to maintain and easier to modify in the future. You don't have to worry about making sure the order you're passing in the values matches up with the order the values are referenced in the future. It is still supported in the latest versions of Python<jupyter_code>name = 'Bob'
errno = 156344
print('Hello, %s' % name)

## Hexadecimal parser
print('Hello, 0x%x' % errno)

## Format string by name
print('Hey, %(name)s, there is a 0x%(errno)x error!' % {'name': name, 'errno': errno})<jupyter_output>Hello, Bob
Hello, 0x262b8
Hey, Bob, there is a 0x262b8 error!<jupyter_text>'*New Style*' String formattingPython3 introduced a new way to do string formatting that was also later back-ported to Python2.7. Formatting is now handled by calling a *format()* on a string object.Or, you can refer to your variable substitutions by name and use them in any order you want. This is quite a powerful feature as it allows for re-arranging the order of display without changing the arguments passed to the format function.This also shows that the syntax to format an int variable as a hexadecimal string has changed. Overall, the format string syntax has become more powerful without complicating the simpler use cases. It pays off to read up on this *string formatting mini-language* in the Python documentation<jupyter_code>print('Hello, {}'.format(name))

print('Hey, {name}, there is a 0x{errno:x} error!'.format(name=name, errno=errno))<jupyter_output>Hello, Bob
Hey, Bob, there is a 0x262b8 error!<jupyter_text>Literal String Interpolation (Python 3.6+)Python 3.6 adds yet another way to format strings, called *Formatted String Literals*. This new way of formatting strings lets you use embedded Python expressions inside string constants.String literals also support the existing format string syntax of the str.format() method.<jupyter_code>print(f'Hello, {name}')

print(f'Hey, {name}, there is a {errno:#x} error!')<jupyter_output>Hello, Bob
Hey, Bob, there is a 0x262b8 error!<jupyter_text>Template StringOne more technique for string formatting in Python is Template Strings. It's simpler and less powerful mechanism, but in some cases this might be exactly what you're looking for. Template strings are not a core language feature but they're supplied by a module in the standard library. Another difference is that template strings don't allow forma specifiers.That worked great but you're probably wondering when you use template strings in your Python programs. This is the best use case for template strings is when you're handlings forma strings generated by users of your program.<jupyter_code>from string import Template

temp = Template('Hey, $name!')
print(temp.substitute(name=name))

templ_string = 'Hey $name, there is a $error error!'

print(Template(templ_string).substitute(name=name, error=hex(errno)))<jupyter_output>Hey, Bob!
Hey Bob, there is a 0x262b8 error!
{'fraction_non_alphanumeric': 0.06905444126074499, 'fraction_numerical': 0.01174785100286533, 'mean_word_length': 5.549718574108818, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '309901', 'license': 'MIT', 'n_tokens_mistral': 971, 'n_tokens_neox': 903, 'n_words': 481, 'path': 'Patterns for Cleaner Python/String-formatting.ipynb', 'repo_name': 'jsparraq/python-tricks', 'size': '3490'}