lstrip unexpected output: removes additional character [duplicate] - python

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/','')

Related

How exactly the rstrip() function works in Python [duplicate]

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.

Is there a reason that .str.split() will not work with '$_'? [duplicate]

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.

python strip function gives unexpected result [duplicate]

This question already has answers here:
How do I remove a substring from the end of a string?
(23 answers)
Closed 5 years ago.
I want to strip the substring '_pf' from a list of strings. It is working for most of them, but not where there is a p in the part of the string I want to remain. e.g.
In: x = 'tcp_pf'
In: x.strip('_pf')
Out:
'tc'
I would expect the sequence above to give an output of 'tcp'
Why doesn't it? Have i misunderstood the strip function?
you can use:
x = 'tcp_ip'
x.split('_ip')[0]
Output:
'tcp'
You can also use spilt function like below,
x.split('_pf')[0]
It will give you tcp.

What are () (parentheses) are for in regex python [duplicate]

This question already has answers here:
Python regex -- extraneous matchings
(5 answers)
Closed 6 years ago.
I searched in all the internet and didnt get a good answer on this thing.
What parentheses in python are stand for? its very wierd..
For example, if i do:
re.split(r'(/s*)', "ho from there")
its will give me a list of separate words with the spaces between that... how does its happening?
This isn't specific to python, but in regex those denote a capture group.
Further information on how these are handled in re.split can be seen here

how do i remove the first characters of a string [python] [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
thanks to answers on this site I now know that you can remove the last characters of a string by string[:-1] which was really helpfull, however I need to be able to remove the first aswell and as far as I understand this technique it is not possible. so are there other ways to remove parts of strings without replacing spesific letters?
What do you mean "it is not possible"? :)
It is perfectly possible with Explain Python's slice notation:
>>> mystr = 'abcde'
>>> mystr[1:] # Remove the first
'bcde'
>>> mystr[1:-1] # Remove the first and the last
'bcd'
>>> mystr[2:-2] # Remove the first two and the last two
'c'
>>>
string[1:]
You may need to read some documentation. :)

Categories