python strip function gives unexpected result [duplicate] - python

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.

Related

remove the [''] when printing a list in python [duplicate]

This question already has answers here:
Print list without brackets in a single row
(14 answers)
Closed 5 months ago.
For some reason when I print a list like
list = []
list.append("0")
list.append("1")
print(list[0])
the output will be ["0"]
My actual code is a large block of text. Here's a link to the actual code: https://pastebin.com/Z54NfivR
Try this:
print(*list)
This essentially unpacks your list and its elements are treated as if they were separated by commas in the print function.
I used the name list because that was included in your example but it is a good practice to avoid using python commands as variable names.

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.

lstrip unexpected output: removes additional character [duplicate]

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

Split string when ";" appears [duplicate]

This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 5 years ago.
I need to split the string everytime ; shows up.
words = "LightOn;LightOff;LightStatus;LightClientHello;"
Output should be something like this:
LightOn
LightOff
LightStatus
LightClientHello
Simply, everytime it finds ; in a string, it has to split it.
Thank you for help
res = words.split(";")
Refer to this link for more information on split.

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