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()
:
keyurratanghayra on April 20, 2020
Thanks for this wonderful tutorial.
While I tried string stripping with the following example:
The output was
ython
and notpython
.