0

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?

2
  • Have you tried replacing your spaces in your first attempt with \t? Aligns them for me at least, so: "{name1}'s Cell:\t{phone_number1}\n{name2}'s Cell:\t{phone_number2}".format(**locals()) Commented Mar 21, 2018 at 18:52
  • That assumes the everything in the column fits before the next tab stop. Otherwise, you have to start messing with where the tab stops are actually set, which is terminal-dependent. Commented Mar 21, 2018 at 18:57

2 Answers 2

3

You can use nested format specifiers, an often overlooked feature:

max_length = max(len(name1), len(name2)) + 2

string = """{name1:<{max_length}}{phone_number1}
{name2:<{max_length}}{phone_number2}""".format(**locals())

print(string)
# output:
# Nadya  (415) 123-4567
# Jim    (617) 123-4567

The documentation only mentions them in passing, which probably explains why they're relatively unknown:

A format_spec field can also include nested replacement fields within it. These nested replacement fields may contain a field name, conversion flag and format specification, but deeper nesting is not allowed. The replacement fields within the format_spec are substituted before the format_spec string is interpreted. This allows the formatting of a value to be dynamically specified.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is another solution using f-strings, following https://www.python.org/dev/peps/pep-0498/#format-specifiers:

name1 += "'s Cell:"
name2 += "'s Cell:"
max_length = max(len(name1), len(name2))

string = f"{name1:{max_length+2}}{phone_number1}\n{name2:{max_length+2}}{phone_number2}"
print(string)

which produces

Nadya's Cell:  (415) 123-4567
Jim's Cell:    (617) 123-4567

as required.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.