Search code examples
pythonstringstring-formattingf-string

Text formating python


I was wondering if I can use a simpler method to print("{:<3} {:<30} {:<30}".format(x+1, cam, img)).

I'm using print(f"including a variable named var : {var}")

instead of print("including a variable named var :{}".format(var))

But with the f"" I can't find a method to put minimal margin unlike "{:<30}".format(var)

Thanks for help !


Solution

  • You can add the variable name before the : character to get the same result:

    >>> name = "Tom"
    >>> print(f"{name:>3} {name:>30}")
    
    Tom                            Tom