I'm trying to figure out a function that does the opposite of strip for my Python class.
The exact question is:
What function can be used to surround a string with spaces in order to make it a certain length?
Thanks!
L-Strip – lstrip () does the opposite of the R-strip. It removes the whitespaces from the beginning of the string, which is the left side. The strip method will remove any trailing or leading of a given string including tab ( ) and return a new string.
The strip (), lstrip () and rstrip () are some of the methods discussed. We also learned how to Python Trim a string according to our need. If you still have any doubts do let us know in the comments.
Python string | strip () Difficulty Level : Easy. Last Updated : 20 May, 2021. strip () is an inbuilt function in Python programming language that returns a copy of the string with both leading and trailing characters removed (based on the string argument passed). Syntax:
We take a string that has some single white spaces at the start and end of the string. In the above example, we have a total of five spaces. Three between Welcome to Python Pool and two at the beginning and end. When we apply strip () method these three space between the string remains untouched.
The "opposite" of strip
is the center
method:
>>> 'Hello, World!'.center(30)
' Hello, World! '
You can specify the fill character to use as second argument. The default is whitespace.
There are also ljust
and rjust
that will left-justify and right-justify the text up to a certain length, and as such could be considered "opposites" of lstrip
and rstrip
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With