Pythongirl
Pythongirl

Reputation: 21

Understanding `%-14s%` and `%2s%` when formatting string literals

Hi I am quite new at python I just an explanation of what the %-14s% and %2s% does and if so, can you change it to something else or are those nr specific?

def __str__(self):

    return "%-14s"%self.Namn  + "%2s"%self.Spelade_matcher + "%2s"%self.Vinster + "%2s"%self.Oavgjorda +"%2s"%self.Förluster + "%3s"%self.Gjorda_mål +"%3s"%self.Insläppta_mål  + "%2s"%self.Poäng

Upvotes: 1

Views: 4235

Answers (1)

Szabolcs
Szabolcs

Reputation: 4106

That is the old stlye string formatting of Python. See it in details here: https://pyformat.info/

To be more specific about your question: "%-14s" will pad your string to 14 characters and align your content to the left, for example "%-14s" % ('blabla') is gonna be: 'blabla '.

While "%2s" will pad your string to 2 characters and by default align your content to the right.

Upvotes: 3

Related Questions