<jupyter_start><jupyter_text>Why do we care about formatting string output?Every program produces output, but without proper formatting so the user can understand it it's essentially worthless.We're using the store example again, I know you hate it by now. You've properly implemented the loyalty program that runs for every customer. The boss is happy with the results, but doesn't fully trust your work. Your manager is very meticulous and  wants you to give him a print out of every customer name and phone number in the format "The customer's name is <NAME> and their phone number is 300-661-0002". This is where string formatting will make this a very easy job.-----  Table of Contents1. `print` Statements2. `%s`, `%d`, and `%f` Arguments3. `.format()`     3.a. No Parameters        3.b. With Parameters4. Number Formatting5. Formatted String Literals "f-strings"  1. `print` Statements<jupyter_code>print('a')
print 'a'  # allowed in python 2 (now deprecated)
num = 4
lst = [4,3,2]
dic = {'a': 1, 'b': 2}

print(num)
print(lst)
print(dic)<jupyter_output>4
[4, 3, 2]
{'a': 1, 'b': 2}<jupyter_text>2. `%s`, `%d`, and `%f` Arguments OLD method.- %s : str- %d : int- %f : float<jupyter_code>name = '<NAME>'
amt = 10_000.123

print('Customer name is %s and he spent %d' % (name, amt))
print('Name is %s and they spent %f' % (name, amt))
# 2 arguments, but only one passed
print('Name is %s and they spent %f' % (name))
# 1 arguments, but two passed
print('Name is %s and they spent 10000' % (name, amt))
# name is not a float `%f`
print('Name is %s and they spent %f' % (name, name))<jupyter_output><empty_output><jupyter_text>Using the `%` operator to format strings is unforgiving. Strings can't be substituted for numbers and vice-versa. The number of arguments must match as well.<jupyter_code>amt1 = 100
amt2 = 120.25
print('Name is %s and they spent %f and %d' % (name, amt1, amt2))<jupyter_output>Name is <NAME> and they spent 100.000000 and 120<jupyter_text>However, you can use an `int` for a `%f` operator for `float`, and a `float` for a `%d` operator for `int`.  3. `.format()`  Modern version of `%` operator.  3.a. No Parameters<jupyter_code>name = '<NAME>'
amt = 10_000.123

print('Name is {} and they spent {}'.format(
    name, amt))
print('Name is {} and {} spent {}'.format(
    name, name, amt))
# no issues
print('Name is {} and they spent {}'.format(
    name, name, amt))
# error
print('Name is {} and they spent {}'.format(
    name))<jupyter_output><empty_output><jupyter_text>You can have more values in the `.format()` than arguments `{}` in the original `str`, but the reverse will give you an error. More forgiving than `%` operator.  3.b. With Parameters<jupyter_code>name = '<NAME>'
name2 = 'Jessica'
amt = 10_000.123

print('Name is {cust_name} and they spent {amt}'.format(
    cust_name=name, amt=amt))

# order does not matter for keyword arguments
print('Name is {cust_name} and they spent {amt}'.format(
    amt=amt, cust_name=name))
# mixing keyword and non-keyword arguments
print('Name is {cust_name} and {} spent {amt}'.format(
    name2, cust_name=name, amt=amt))
print('Name is {cust_name} and {} spent {amt}'.format(
    cust_name=name, name2, amt=amt))<jupyter_output><empty_output><jupyter_text>You don't follow the order of the `{}` if you're mixing keyword and no-keyword arguments. The non-keyword `{}` arguments must always precede keyword-arguments `{cust_name}`.  4. Number Formatting<jupyter_code>num1 = 100
num2 = 100.123456

print('%.2f' % num1)  # old method
print('{:0.3f}'.format(num2))  # modern method<jupyter_output>100.00
100.123<jupyter_text>5. Formatted String Literals "f-strings"Two key points are:1. `f` precedes the string quotations2. keyword arguments, ex: `{cust_name}` must exactly match the variable name, ex: `cust_name`<jupyter_code>cust_name = '<NAME>'
phone_num = '300-661-0002'

print(f'The customer\'s name is {cust_name} and ' + \
      f'their phone number is {phone_num}')<jupyter_output>The customer's name is <NAME> and their phone number is 300-661-0002
{'fraction_non_alphanumeric': 0.13366336633663367, 'fraction_numerical': 0.036386138613861384, 'mean_word_length': 4.651748251748252, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '643988', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1475, 'n_tokens_neox': 1374, 'n_words': 515, 'path': 'Lesson 8 - String Formatting/Python String Formatting.ipynb', 'repo_name': 'programmingwithalex/-intro_to_python_tutorials', 'size': '4054'}