Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

String Formatting

Give Feedback

In this lesson, you’ll explore string methods that modify or enhance the format of a string:

  •<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

>>> s = 'spam'
'   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, '-')
>>> s.ljust(3, '-')

Here’s how to use str.rjust():

>>> s = 'spam'
>>> s.rjust(10)
'      spam'
>>> s.rjust(10, '-')
>>> s.rjust(3, '-')

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 = ''
>>> link.lstrip('/:pth')

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(';$.')

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 = ''
>>> link.strip('w.moc')
>>> link.strip(':/pth w.moc')

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)
>>> s.zfill(10)
>>> s = '+42'
>>> s.zfill(5)
>>> s = '-51'
>>> s.zfill(3)

>>> s = 'spam'
>>> s.zfill(8)

keyurratanghayra on April 20, 2020

Thanks for this wonderful tutorial.

While I tried string stripping with the following example: link = ‘‘ print(link.strip(‘http://’))

The output was ython and not python.

Chris Bailey RP Team on April 20, 2020

Hi @keyurratanghayra,

In your link example with link=' 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:\\'

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.

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).

kiran on July 23, 2020

can you provide any best resource to know more about(deep dive) for strip() in python.

Chris Bailey RP Team on July 23, 2020

Hi @manupanduworld, I don’t know of a specific deep dive, but I will give you some resources that I think may help you.

The first is the article this course is based on.

The second is the Python3 documentation for .strip()

The third is a proposal for the new version of Python, Python 3.9 that adds new methods that work more specifically on removing text from the beginning or ending of a string. Which has been an area some students get stuck on. It is PEP 616 - and it calls for adding 2 methods named, .removeprefix() and removesuffix(). Here is an excerpt from the PEP (Python Enhancement Proposal).

The main opportunity for user confusion will be the conflation of lstrip/rstrip with removeprefix/removesuffix. It may therefore be helpful to emphasize (as the documentation will) the following differences between the methods:

(l/r)strip: The argument is interpreted as a character set. The characters are repeatedly removed from the appropriate end of the string. remove(prefix/suffix): The argument is interpreted as an unbroken substring. Only at most one copy of the prefix/suffix is removed.

I hope this helps to explain it a bit further.

Alain Rouleau on July 29, 2020

I like the zfill() method. As you mentioned, very handy for indexes. Good for sorting properly as well. You know, ‘07’, ‘08’, ‘10’ as opposed to ‘10’, ‘7’, ‘8’. Will be using that one more often. I knew about it before but completely forgot, so, thanks.

marciolazaijunior on Aug. 17, 2020

Hello, can you tell me which IDE are you using?

Chris Bailey RP Team on Aug. 18, 2020

Hi @Marciolazaijunior, I’m using VSCode. I have a color theme installed called Dainty - Material Theme Ocean. I use a separate tool for interactions in the REPL called bPython. You can find more information and links at the bottom of the 2nd lesson in this course.

Become a Member to join the conversation.