1

This may be quite a vague question so i'll try to explain the best way i can. Say if i have 2 variables (with different sized length)

variable_one = "John Doe"
variable_two = "Thomas Smith"

How would i print them in such a way that the output would be consistently spaced in between each variable without knowing the exact length of the strings? For Example if i print them out normally with things in the middle:

print(variable_one + " - Age: 40")
print(variable_two + " - Age: 2")

The output would be:

John Doe - Age: 40
Thomas Smith - Age: 2

How would i automatically make the output consistent with spaces so it would look something like this:

John Doe      -      Age: 40
Thomas Smith  -      Age: 2

Thank you :) ~ Pete

meep
  • 334
  • 3
  • 10
  • Try `print(variable_one + "\t\t-\tAge:\t40")` – Haris Aug 16 '18 at 16:21
  • 2
    Possible duplicate of [How to format print output or string into fixed width?](https://stackoverflow.com/questions/8450472/how-to-format-print-output-or-string-into-fixed-width) – pault Aug 16 '18 at 16:21
  • You don't need to do it "without knowing the exact length of the strings" if you check the exact length of the strings. – zvone Aug 16 '18 at 16:22
  • @Haris That will work only if the difference is less than tab size, and even then not always – zvone Aug 16 '18 at 16:23
  • I see. But isn't "\t" just tab? Plus, those two variables, i would know the size. If i was collecting data from somewhere else, I wouldn't know the size. Thanks for your help. – meep Aug 16 '18 at 16:25
  • which version of python are you using? @petrexxy – CoffeeTableEspresso Aug 16 '18 at 16:25
  • in this "similar" question [this answer](https://stackoverflow.com/a/26937531/4020610) presents a few libraries that might help, otherwise you'll have to find the "max length" of each "variable length" "column" and use "parameterized string formatting" (stuff like `"{:10s}".format(x)` or `"%10s" % x`) to do the job, which most likely is what these libraries are doing too. – Lohmar ASHAR Aug 16 '18 at 16:39

3 Answers3

2

Try using format:

print('{0:12s} - Age {1:<d}'.format(var, age))

12 in this case is how much space you want the name to take up (filled with spaces on the right to fill the extra space).

If you are using python3.6 or greater, you can also use formatted strings:

print(f'{var:12s} - Age {age:<d}')

The above should give the same as using format, but I find it to be more readable.

Also, the documentation for formatted strings and format: https://docs.python.org/2/library/string.html#format-specification-mini-language, which should let you adjust the format if none of the answers here are exactly what you want.

CoffeeTableEspresso
  • 2,614
  • 1
  • 12
  • 30
1

You could try string formatting

"{:10s} {:s}".format(variable1, " - Age 40")

where 10 is the number of characters you want your variable1 to take up. If its length is less than that, it is padded with spaces. Similarly for the second string.

However, your - hyphen is part of the second string, so it can't be padded, unless you split your string manually.

blue_note
  • 27,712
  • 9
  • 72
  • 90
0

If you know all the variables you need to print beforehand and they're stored in vars, with the ages stored in ages you can use string formatting like:

max_len = max(map(len, vars))
for name, age in zip(vars, ages)
     print("{0:{1}} - Age: {2}".format(name, max_len, age))

If they were stored as objects with name and age fields, then

max_len = max([len(var.name) for var in vars])
for var in vars
     print("{0.name:{1}} - Age: {0.age}".format(var, max_len))

If you don't know all the variables beforehand, you can do the same thing with the width of the padded name hardcoded:

print("{0:10} - Age: {1}".format(name, age))
natonomo
  • 325
  • 1
  • 9