C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python program that uses ljust, rjust
s = "Paris"
# Justify to left, add periods.
print(s.ljust(10, "."))
# Justify to right.
print(s.rjust(10))
Output
Paris.....
Paris
Here: We right-align (rjust) a string with spaces. Then we left-align (ljust) a number with star characters.
Python program that uses format padding
# Right-align a string in 10 chars.
# ... First char in format string is a space.
value = format("line 1", " >10s")
print(value)
# Left-align a number in 10 chars.
# ... Pad with a star character.
print(format(100, "*<10d"))
Output
line 1
100*******