'How to remove whitespaces at certain sections of a text in Python?
I have a text which looks like below
High MM Pol Ag to SO Pol Ag
As you can see, there is a whitespace in the beginning, then a word and again some whitespace and then the rest of the text.
What I want is this piece of text MM Pol Ag to SO Pol Ag.
Now I can use strip to remove the leading and ending whitespaces like below.
text = text.strip()
High MM Pol Ag to SO Pol Ag
But the white space after High is varying ie it can sometimes be two spaces or four or maybe more.
How can I get the required text?
Note: The text can vary.
Solution 1:[1]
split() takes a maxsplit argument. This lets you split off just one piece of text and not split the rest:
s = " High MM Pol Ag to SO Pol Ag"
val = s.split(maxsplit=1)[1]
print(val)
# 'MM Pol Ag to SO Pol Ag'
This lets you avoid creating a temp list and rejoining it to a string.
Solution 2:[2]
Here you go (use the split() method to get an array of strings):
s = "High MM Pol Ag to SO Pol Ag"
s_new = ''.join([str(elem)+' ' for elem in s.split(' ')[5:]])
print(s_new)
Output:
MM Pol Ag to SO Pol Ag
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mark |
| Solution 2 |
