I'm trying to generate a string which looks like this:
Nadya's Cell: (415) 123-4567
Jim's Cell: (617) 123-4567
where the names and phone numbers vary and need to be interpolated in, and the phone numbers should be aligned. For this example, I've used the following template:
name1 = "Nadya"
name2 = "Jim"
phone_number1 = "(415) 123-4567"
phone_number2 = "(617) 123-4567"
string = "{name1}'s Cell: {phone_number1}\n{name2}'s Cell: {phone_number2}".format(**locals())
Instead of adding the spaces in manually, I'd like the total width of the string to adapt to the longest of name1 and name2.
So far, all I've been able to come up with following https://docs.python.org/3.6/library/string.html is the following:
max_length = max(len(name1), len(name2))
# This should use max_length and contain "'s Cell:"
string = "{name1:<7}{phone_number1}\n{name2:<7}{phone_number2}".format(**locals())
print(string)
which produces
Nadya (415) 123-4567
Jim (617) 123-4567
The problem is that the total width, 7, is still hard-coded into the template, and I don't see how to add the 's Cell: after the name because this produces spaces between the name and the apostrophe. Any ideas how to tackle this?
\t? Aligns them for me at least, so:"{name1}'s Cell:\t{phone_number1}\n{name2}'s Cell:\t{phone_number2}".format(**locals())