<jupyter_start><jupyter_text>Playing with New String Formattingsee: https://pyformat.info/<jupyter_code>from numpy import pi as π<jupyter_output><empty_output><jupyter_text>Right justify<jupyter_code>print('{:>10}'.format('test'))<jupyter_output>test<jupyter_text>Default is to left justify (at last)<jupyter_code>print('{:10}'.format('test'))<jupyter_output>test<jupyter_text>Center<jupyter_code>print('{:^10}'.format('test'))<jupyter_output>test<jupyter_text>Custom character to fill<jupyter_code>print('{:_^10}'.format('test'))<jupyter_output>___test___<jupyter_text>Formatting Numbers<jupyter_code>print('{:48.32f}'.format(π))<jupyter_output>3.14159265358979311599796346854419<jupyter_text>Padding<jupyter_code>print('{:048.32f}'.format(π))<jupyter_output>000000000000003.14159265358979311599796346854419<jupyter_text>Signed Numbers: add a + or space<jupyter_code>print('{:+48.32f}'.format(π))
print('{: 48.32f}'.format(π))
print('{: 48.32f}'.format(-π))<jupyter_output>+3.14159265358979311599796346854419
              3.14159265358979311599796346854419
             -3.14159265358979311599796346854419<jupyter_text>Named place holders<jupyter_code>data = {'first': 'Hodor', 'last': 'Hodor!'}
print('{first} {last}'.format(**data))

print('{first} {last}'.format(first='Hodor', last='Hodor!'))<jupyter_output>Hodor Hodor!
Hodor Hodor!<jupyter_text>Accessing items<jupyter_code>data = [4, 8, 15, 16, 23, 42]

print('{d[4]} {d[5]}'.format(d=data))<jupyter_output>23 42<jupyter_text>Can easily format dates<jupyter_code>from datetime import datetime
print('{:%Y-%m-%d %H:%M:%S}'.format(datetime.today()))<jupyter_output>2018-06-07 13:03:09<jupyter_text>Parameterized formatting<jupyter_code>print('π = {:{sign}{width}.{precision}f}'.format(π,width=4,sign='+',precision=4))<jupyter_output>π = +3.1416
{'fraction_non_alphanumeric': 0.18378678511937813, 'fraction_numerical': 0.13603553581343697, 'mean_word_length': 11.871428571428572, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 39, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '345452', 'license': 'MIT', 'n_tokens_mistral': 917, 'n_tokens_neox': 750, 'n_words': 69, 'path': '4-assets/BOOKS/Jupyter-Notebooks/Overflow/string_formatting.ipynb', 'repo_name': 'impastasyndrome/Lambda-Resource-Static-Assets', 'size': '1801'}