This question already has answers here:
Strip removing more characters than expected
(2 answers)
Remove certain word from string
(4 answers)
Closed last month.
Python's rstrip() function not returned output as expected. I would like to know
the functionality behind this function.
my_str = 'cisco.com'
print(my_str.rstrip(".com")
Expecting this result should be cisco but it returning cis.
str.rstrip([chars])
The chars argument is not a suffix; rather, all combinations of its values are stripped:
So you're passing 4 chars: ., c, o, m.
Since there's a o before the ., that's also removed as you provided the o as a remove char.
Related
This question already has an answer here:
Python string literal concatenation
(1 answer)
Closed 2 years ago.
print('Hello''World')
>>> HelloWorld
How come this works, when multiple string arguments are not separated by comma?
I found this unusual because, like any other function multiple parameters must be separated with commas.
There should have been a syntax error.
In Python, adjacent string literals are concatenated by default.
s = 'Hello' "world" '''!''' """?"""
This is perfectly fine. I used different quotes for each literal, and even separate them with spaces, and everything is still fine.
This question already has answers here:
Why doesn't regular expression alternation (A|B) match as per doc?
(3 answers)
Closed 3 years ago.
I want to clarify a doubt in python - regular expression
import re
stri="Item3. Super Market ListsItem4"
#1st print
print(re.sub(r'(Item[0-9]|Item[0-9]\.)', "", stri,))
#2nd print
print(re.sub(r'(Item[0-9]\.|Item[0-9])', "", stri,))
In the stri, I need to remove the "Item4" and "Item3."
output -
'. Super Market Lists'
' Super Market Lists'
My question is, I used OR(|) operator for both patterns.
In the 1st print statement, it did not remove the dot(.) in the given string. And in the 2nd print statement, I switched the pattern with OR operator. In this time, it removed the dot(.) in the string. Why it happens like this
Thank you
It happens because it first tries to match the left operand of the OR operator.
Because it matches without the dot, it removes the matched part without looking into the right operand.
This question already has answers here:
Split Strings into words with multiple word boundary delimiters
(31 answers)
Closed 3 years ago.
I am trying to split a string using .str.split('$_') but this is not working.
Other combinations like 'W_' or '$' work fine but not '$'. I also tried .str.replace('$') - which also does not work.
Initial string is '$WA:G_COUNTRY'
using
ClearanceUnq['Clearance'].str.split('$_')
results in [$WA:G_COUNTRY]
no split....
whereas
ClearanceUnq['Clearance'].str.split('$')
results in [, WA:G_COUNTRY]
as expected
This is because it is trying to split the string when it finds a $ AND a _ right next to eachother, which does not occur in your first string.
This question already has answers here:
How to replace two things at once in a string?
(6 answers)
Closed 4 years ago.
Because of this answer, it might be necessary to do this:
path = r"D:\Temp\abc[def]\ # (i have many others to process)
path = path.replace('[', '[[]').replace(']', '[]]')
# now we can use glob here with path
However, the first replace gets mixed up with the second replace, and the result is not what is expected, i.e. D:\Temp\abc[[]def[]]\.
The only solution I found is to use a temporary character ~ to avoid the 2 replace to be mixed up with each other:
path = path.replace('[', '[[~').replace(']', '[]]').replace('~', ']')
Is there a nice way to use 2 replacements without the first having effect on the second?
You don't need to replace ].
Special characters to replace are only '?', '*' and '[', and are handled by glob.escape.
>>> print(glob.escape(path))
D:\Temp\abc[[]def]\
This question already has answers here:
Python string.strip stripping too many characters [duplicate]
(3 answers)
Closed 5 years ago.
lstrip removed additional character, please help me understand it why. Is it removing all the input characters from the beginning?
'http://twitter.com/c_renwick'.lstrip('http://twitter.com/')
>>>'_renwick'
lstrip takes a list of characters to remove from the string. As c is in the list you provided, it gets removed
To achieve what you actually want, use replace:
'http://twitter.com/c_renwick'.replace('http://twitter.com/','')