python string strip weird behavior [duplicate] - python

This question already has answers here:
How do the .strip/.rstrip/.lstrip string methods work in Python?
(4 answers)
Closed 4 years ago.
Is there a reason why I am having this kind of string strip behavior ? Is this a bug or some string magic I am missing
# THIS IS CORRECT
>>> 'name.py'.rstrip('.py')
'name'
# THIS IS WRONG
>>> 'namey.py'.rstrip('.py')
'name'
# TO FIX THE ABOVE I DID THE FOLLOWING
>>> 'namey.py'.rstrip('py').rstrip('.')
'namey'

That's because the str.rstrip() command removes each trailing character, not the whole string.
https://docs.python.org/2/library/string.html
string.rstrip(s[, chars])
Return a copy of the string with trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the end of the string this method is called on.
This also generates same result
>>> 'nameyp.py'.rstrip('.py')
'name'
You could try str().endswith
>>> name = 'namey.py'
... if name.endswith('.py'):
... name = name[:-3]
>>> name
'namey'
Or just str().split()
>>> 'namey.py'.split('.py')[0]
'namey'

Related

How to split my string "a!b!" into a!, b! in python? [duplicate]

This question already has answers here:
Python split() without removing the delimiter [duplicate]
(4 answers)
Closed 11 months ago.
Is it possible to separate the string "a!b!" into two strings "a!" and "b!" and store that in a list? I have tried the split() function (and even with the delimiter "!"), but it doesn't seem to give me the right result that I want. Also, the character "!" could be any character.
How about :
string = 'a!ab!b!'
deliminator = '!'
word_list = [section+deliminator for section in string.split(deliminator) if section]
print(word_list)
Output :
['a!', 'ab!', 'b!']
split() is used when you need to seperate a string with particular character. If you want split a string into half, Try this
s = "a!b!"
l = [s[ : len(s)//2], s[len(s)//2 : ]]
# output : ["a!", "b!"]

How to replace the multiple different words with a single character/word in Python? [duplicate]

This question already has answers here:
Better way to remove multiple words from a string?
(5 answers)
Closed 3 years ago.
Note: Without chaining replace method (or) looping the characters in for loop (or) list comprehension
input_string = "the was is characters needs to replaced by empty spaces"
input_string.replace("the","").replace("was","").replace("is","").strip()
output: 'characters needs to replaced by empty spaces'
Is there any direct way to do this?
You can use python regex module(re.sub) to replace multiple characters with a single character:
input_string = "the was is characters needs to replaced by empty spaces"
import re
re.sub("the|was|is","",input_string).strip()
'characters needs to replaced by empty spaces'
This should help..
input_string = "the was is characters needs to replaced by empty spaces"
words_to_replace=['the', 'was','is']
print(input_string)
for words in words_to_replace:
input_string = input_string.replace(words, "")
print(input_string.strip())

Split the string every special character with regular expressions [duplicate]

This question already has answers here:
What are non-word boundary in regex (\B), compared to word-boundary?
(2 answers)
Closed 3 years ago.
I want to split my string into pieces but every some text and a special character. I have a string:
str = "ImEmRe#b'aEmRe#b'testEmRe#b'string"
I want my string to be split every EmRe#b' characters as you can see it contais the ' and that's the problem.
I tried doing re.split(r"EmRe#b'\B", str), re.split(r"EmRe#b?='\B", str) and also I tried both of them but without the r before the pattern. How do I do it? I'm really new to regular expressions. I would even say I've never used them.
Firstly, change the name of your variable, since str() is a built-in Python function.
If you named your variable word, you could get a list of elements split by your specified string by doing this:
>>> word = "ImEmRe#b'aEmRe#b'testEmRe#b'string"
>>> word
"ImEmRe#b'aEmRe#b'testEmRe#b'string"
>>> word.split("EmRe#b'")
['Im', 'a', 'test', 'string']
Allowing you to use them in many more ways than just a string! It can be saved to a variable, of course:
>>> foo = word.split("EmRe#b'")
>>> foo
['Im', 'a', 'test', 'string']

Python: Keeping whitespace at the beginning of a string [duplicate]

This question already has answers here:
Grab a line's whitespace/indention with Python
(6 answers)
Closed 5 years ago.
I Have a string that looks like this:
old_string = ' Some_text'
And I want to write a new string, but I would like to keep the same white-space at the beginning.
Is there a way in Python that I can keep this white-space?
The white-space could contain spaces or tabs, but the exact number of tabs or spaces is unknown.
I think this could be done using regex but I'm not sure if there is a way. And since the text in the string is not always the same I can't use
new_string = old_string.replace('Some_text','new_text')
any thoughts would be more than welcome.
You can do:
new_string = old_string[:-len(old_string.lstrip())] + 'new text'
Or if you prefer str.format:
new_string = '{}new text'.format(old_string[:-len(old_string.lstrip())])
Count how many characters get removed when you use lstrip;
str = ' Some_text'
whitespace = len(str) - len(str.lstrip())
print(whitespace)
Outputs;
6
You can use itertools.takewhile() to get the leading whitespace characters:
>>> from itertools import takewhile
>>> old_string = ' Some_text'
>>> whitespace = list(takewhile(str.isspace, old_string))
>>> "".join(whitespace)
' '
>>> len(whitespace)
6
To get the rest of the string you could use itertools.dropwhile():
>>> "".join(dropwhile(str.isspace, old_string))
'Some_text'

Are there limits to using string.lstrip() in python? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is this a bug in Python 2.7?
The .lstrip() function doesn't quite work as I expected on some strings (especially ones with underscores). e.g.,
In [1]: a='abcd_efg_hijk_lmno_pqrs_tuvw_xyz'
In [2]: a.lstrip('abcd_efg')
Out[2]: 'hijk_lmno_pqrs_tuvw_xyz'
Here, the '_' between 'g' and 'h' is missing. Any idea as to why this happens?
.lstrip() doesn't do what you think it does. It removes any of the provided characters from the left end of the string. The second underscore is as much an underscore as the first, so it got removed too.
"aaaaaaaabbbbbbbc".lstrip("ab") # "c"
What you want:
b = 'abcd_efg'
if a.startswith(b):
a = a[len(b):]
As the str.lstrip documentation says,
The chars argument is not a prefix; rather, all combinations of its values are stripped:
>>> 'www.example.com'.lstrip('cmowz.')
'example.com'
To do what you want:
>>> a = "foobar"
>>> sub = "foo"
>>> b = a[len(sub):]
>>> b
'bar'

Categories