rewriting this python .ljust statement into more current form

84 Views Asked by At

i couldn't figure out how to write this another way (or should i say a more current way, like with an f-string or str.format). the goal is to print "Current activity" followed by dashes to a width of 26 characters as defined by a variable.

activityCols = 26
print("Current activity".ljust(activityCols,"-"))

any thoughts on a more current way?

i tried

activityCols = 26
print('{:-<(activityCols)}'.format("Current activity"))

but that didn't work.

2

There are 2 best solutions below

2
nilbarde On BEST ANSWER

.ljust method is good enough,

If you want to try something new

original_string = "Current activity"
length = 26
character_to_append = "-"
result_string = f"{original_string:{character_to_append}<{length}}"

print(result_string)

1
Swifty On

You can use:

activityCols = 26
print(f'{"Current activity":-<{activityCols}}')

Output: Current activity----------