Can a string method, such as .rjust(), be saved in a variable and applied to a string?
Looked here but could not find a solution.
For example, instead of stating rjust(4, '-') twice, can it be coded once in a variable and then passed to the two strings?
# Instead of this
print("a".rjust(4, '-'),
"xyz".rjust(4, '-'),
sep="\n")
# Something like this?
my_fmt = rjust(4, '-')
print("a".my_fmt,
"xyz".my_fmt,
sep="\n")
Both result in:
---a
-xyz
functools.partial, but a lambda is easier. Lookup "Python lambda".str.rjust()'s parameters areself, width, fillchar=' ', /. Normally where the first parameter isself, you'd supply the others as kwargs, but that's not allowed. Also, named lambdas are bad practice; use adefinstead.