String Formatting
In this lesson, you’ll explore string methods that modify or enhance the format of a string:
str.center(<width>[, <fill>])str.expandtabs(tabsize=8)str.ljust(<width>[, <fill>])str.rjust(<width>[, <fill>])str.lstrip([<chars>])str.rstrip([<chars>])str.strip([<chars>])str.replace(<old>, <new>[, <count>])str.zfill(<width>)
Here’s how to use str.center():
>>> s = 'spam'
>>> s.center(10)
' spam '
>>> s.center(10, '-')
'---spam---'
>>> s.center(3, '-')
'spam'
Here’s how to use str.expandtabs():
>>> s = 'a\tb\tc'
>>> s.expandtabs()
'a b c'
>>> s.expandtabs(4)
'a b c'
Here’s how to use str.ljust():
>>> s = 'spam'
>>> s.ljust(10)
'spam '
>>> s.ljust(10, '-')
'spam------'
>>> s.ljust(3, '-')
'spam'
Here’s how to use str.rjust():
>>> s = 'spam'
>>> s.rjust(10)
' spam'
>>> s.rjust(10, '-')
'------spam'
>>> s.rjust(3, '-')
'spam'
Here’s how to use str.lstrip():
>>> s = ' spam bacon egg '
>>> s
' spam bacon egg '
>>> s.lstrip()
'spam bacon egg '
>>> t = ' \t \n spam \t \n egg \t \n '
>>> t
' \t \n spam \t \n egg \t \n '
>>> t.lstrip()
'spam \t \n egg \t \n '
>>> link = 'http://www.realpython.com'
>>> link.lstrip('/:pth')
'www.realpython.com'
Here’s how to use str.rstrip():
>>> s = ' spam bacon egg '
>>> s
' spam bacon egg '
>>> s.rstrip()
' spam bacon egg'
>>> t = ' \t \n spam \t \n egg \t \n '
>>> t
' \t \n spam \t \n egg \t \n '
>>> t.rstrip()
' \t \n spam \t \n egg'
>>> x = 'spam.$$$;'
>>> x.rstrip(';$.')
'spam'
Here’s how to use str.strip():
>>> s = ' spam bacon egg '
>>> s
' spam bacon egg '
>>> s.strip()
'spam bacon egg'
>>> t = ' \t \n spam \t \n egg \t \n '
>>> t
' \t \n spam \t \n egg \t \n '
>>> t.strip()
'spam \t \n egg'
>>> link = 'http://www.realpython.com'
>>> link.strip('w.moc')
'http://www.realpython'
>>> link.strip(':/pth w.moc')
'realpython'
Here’s how to use str.replace():
>>> s = 'spam spam spam egg bacon spam spam lobster'
>>> s.replace('spam', 'tomato')
'tomato tomato tomato egg bacon tomato tomato lobster'
>>> s.replace('spam', 'tomato', 3)
'tomato tomato tomato egg bacon spam spam lobster'
Here’s how to use str.zfill():
>>> s = '42'
>>> s.zfill(5)
'00042'
>>> s.zfill(10)
'0000000042'
>>> s = '+42'
>>> s.zfill(5)
'+0042'
>>> s = '-51'
>>> s.zfill(3)
'-51'
>>> s = 'spam'
>>> s.zfill(8)
'0000spam'
Comments & Discussion
Chris Bailey RP Team on April 20, 2020
Hi @keyurratanghayra,
In your link example with link='www.python.com the ‘p’ from python will be removed as it is one of the characters to be stripped from either side. In the example in the lesson, the link was slightly different.
link = 'http:\\www.realpython.com'
and the characters to be stripped from left and right side were defined as
link.strip(':/pth w.moc') # this would remove the colon, slash, period
# and the letters p, t, h, w, m, o, and c
# from either side.
'realpython'
The extra letters from ‘real’ stop the “stripping” on the left side as the letter “r” is not one of the characters. Where as in your example the letter “p” is one of the letters to be stripped. In fact you do not need the repeated “t” and “/” as only one instance needs to specified for it to strip the 2 t's and 2 /'s. I made a slight mistake in my wording during the lesson, saying it was going to remove the “path”, but what I should have said is it is going to remove those specific characters (/:pth) and I could have put them in a different order, like (p:t/h).
Become a Member to join the conversation.

keyurratanghayra on April 20, 2020
Thanks for this wonderful tutorial.
While I tried string stripping with the following example: link = ‘www.python.com‘ print(link.strip(‘http:// w.com’))
The output was ython and not python.