0

Is it possible to print multiple strings on one line in such a way that the final string will always be x amount of space from the left? For example, is there anyway to print a result similar to this, where strZ is always printed in the same place (not right-justified)?

strA strB strC        strZ 
strA                  strZ 
strC StrB strD strE   strZ
Flimzy
  • 60,850
  • 13
  • 104
  • 147
user2931070
  • 57
  • 2
  • 3

1 Answers1

3

Using str.format:

fmt = '{:<4} {:<4} {:<4} {:<6} {:<4}'
print(fmt.format('strA', 'strB', 'strC', '', 'strZ'))
print(fmt.format('strA', '', '', '', 'strZ'))
print(fmt.format('strA', 'strB', 'strC', 'strE', 'strZ'))

prints

strA strB strC        strZ
strA                  strZ
strA strB strC strE   strZ

See Format String Syntax.

falsetru
  • 314,667
  • 49
  • 610
  • 551