2

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
0

1 Answer 1

4

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.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.