I finished the intro to strings portion of the Udemy course today!
It mostly covered formatting stuffs…
I learned:
When using {} to call a field number there are a ton of little things one can do to format the output. For instance, to have a 4 spaces in the result use {:4} which can be useful for aligning numbers of different length for readability.
To align those values to the left use {:<4}. To align them to the center use {:^4}. If you want to specify digits use {:4f}, with ‘f’ setting the standard of 6 digits. You can even choose to not put any numbers in {} and it will automatically output the fields in whatever order is in the .format().
An additional way to format this is with print(f”hi my name is {name}”) rather than print(“hi my name is {0}”.format(age)). This is a bit cleaner looking as it allows one to directly write the field names.
Apparently there’s this older version of string interpolation that’s used in previous Python versions using %. The concept is similar to {} but it requires the field numbers to be in the same order. So I don’t think you can call a field twice or do it out of order with them.