Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python opposite of strip() [closed]

Tags:

python

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!

like image 854
BryanLavinParmenter Avatar asked Oct 28 '14 19:10

BryanLavinParmenter


People also ask

What is the difference between L strip and R strip in Python?

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.

What are the methods to strip a string in Python?

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.

What is the difficulty level of strip () in Python?

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:

How to strip a string with three spaces in Python?

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.


1 Answers

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.

like image 64
Bakuriu Avatar answered Oct 05 '22 04:10

Bakuriu