Python opposite of strip() [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
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!

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.

format strings
"%50s World"%("Hello")
"%-50s World"%("Hello")
"%^50s World"%("Hello")
or newfangled
"{0:50s} World".format("Hello")
"{0:>50s} World".format("Hello")
"{0:^50s} World".format("Hello")
these have nifty side effects
"%^*s World"%(50,"Hello")

Try s.center(N) where N is the line length you want, s - your string.
For example:
>>> "xx".center(10)
' xx '

Related

Using the strip function to strip integer at the ends of a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to strip 0 from a given string.
The string contains either 1 or 0. I want to strip the zeroes if they appear at the ends.
I know i can do this using if condition, but i want to know if there is any function made to do this efficiently than using if-else.
Example-
String = 0100010101010
Output = 10001010101
Also, i don't think using regex is any more efficient, complexity wise.
Try this:
s = "0100010101010"
print(s.lstrip("0").rstrip("0"))
'10001010101'
This should work for the string s:
s = s.strip("0")
Make sure s is a string and not a number.
Can you try this , it will work
s = str(s).strip("0")

How to keep the only characters I want [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to keep a certain character for my python project which and I don’t want to replace every unused character with ‘’ is there any way to do it?
You can use a function re.sub in re library
For example:
data = re.sub('0123456789abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNNM+/-', '', data)
This will keep every character in the first parameter replace with the second parameter by using String from the third parameter
if you want to keep "d" in the string
origin = "abcdefghidx"
result = "".join([c for c in origin if c=="d"])
You can use str.replace('what to want to delete', 'what you want to add').
ex-
name = "stackoverflow"
newName = name.replace('o', '0')
newName becomes 'stack0verfl0w'

Is there any method to replace space with multiple characters? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Example:
Input : "Base derived derived You are great"
Output: "Base->derived:derived You are great"
Here, the first two spaces are replaced with -> and : respectively, the rest of the string remains the same.
Nothing fancy, but if it's a one-off application, this would do the trick, using the Python str.split() method to create a list, splitting the string into three chunks on the first two spaces, then create a new string with those three chunks separated by '->' and ':'.
your_string = "Base derived derived You are great"
split_string = your_string.split(maxsplit=2)
result = f"{split_string[0]}->{split_string[1]}:{split_string[2]}"
As suggested in comments by #yatu, this can be reduced to a single statement using the *-operator to unpack the list:
result = '{}->{}:{}'.format(*your_string.split(maxsplit=2))
result in both cases:
'Base->derived:derived You are great'

How to remove all characters from a string after two white spaces in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
The string i needed to format is,
string = "How are you? abcdef"
I need to remove the "abcdef" from the string.
string = string.split(' ')[0]
Edit: Explanation. The line of code above will split the single string into a list of strings wherever there is a double space. It is important to note that whatever is split upon, will be removed. [0] then retrieves the first element in this newly formed list.

How to remove periods from the middle of sentences [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Is it possible to remove periods from the middle of a string (sentence), leaving the ending period?
The answers that I have seen, basically strip all of the periods.
Remove periods at the end of sentences in python
If I understand correctly, this should do what you want:
import re
string = 'You can. use this to .remove .extra dots.'
string = re.sub('\.(?!$)', '', string)
It uses regex to replace all dots, except if the dot is at the end of the string. (?!$) is a negative lookahead, so the regex looks for any dot not directly followed by $ (end of line).

Categories

Resources