Skip to content

Python justify string

Python justify center

python
>>> 'hello'.center(10)
'  hello   '

Python justify left

python
>>> 'hello'.ljust(10)
'hello     '

Python justify right

python
>>> 'hello'.rjust(10)
'     hello'

Python justify center with fillchar

python
>>> 'hello'.center(10, '-')
'--hello---'

Python justify left with fillchar

python
>>> 'hello'.ljust(10, '-')

'hello-----'

Python justify right with fillchar

python

>>> 'hello'.rjust(10, '-')
'-----hello'