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

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. :)

Related

How to drop string after certain point in all elements of list? [duplicate]

This question already has answers here:
How do I remove a query from a url?
(6 answers)
Closed 4 years ago.
I have a large string stored in a list. I only need to extract the URL and dump the rest of the string. How would I do this? The bold is all I need and for every element in the list, the ? is the cut off where I want to capture the URL. I am not sure if this will the 'u' disappear as well.
lst =[u'https://images.com/candles.jpg?asdkfasdkfihawklwie']
Just want to emphasis that I want to drop everything after the question mark including the question mark itself for every element in the list. Only need the URL.
Thank you in advance.
Use split with list comprehension:
L = [x.split('?', 1)[0] for x in lst]
As #Martijn Pieters commented faster is use partition:
L = [x.partition('?')[0] for x in lst]

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

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.

Removing characters from the left side of a Python string? [duplicate]

This question already has answers here:
How to remove the left part of a string?
(21 answers)
Closed 6 years ago.
How would I go about removing characters from the left side of a Python string? Example:
string = "Devicename MA:CA:DD:RE:SS"
What should I do to make a new string of just the MAC Address?
You can do different things:
string.split(' ')[1]
or
string[11:]
or
string[-14:]
both yielding
'MA:CA:DD:RE:SS'
The last option is the closest to what you want I suppose. It takes the leftmost 14 characters from the string.
Assuming that the string always has the format:
Devicename<space>MAC Address
You can do this by simply splitting the string on the space and taking the second element in the resulting list.
>>> string = "Devicename MA:CA:DD:RE:SS"
>>> string.split()[1]
'MA:CA:DD:RE:SS'
One note - I assume you know that is not a valid MAC address, correct?

Python strip a string from index [duplicate]

This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Closed 9 years ago.
I have a list of strings in the format: 'foo7bar'. How is it possible in Python to remove the 7 along with any characters that follow?
This is similar to this question, but I need the answer for Python.
You can do this using Python's slice notation:
>>> mystr = 'foo7bar'
>>> mystr[:mystr.index('7')]
'foo'
>>>
The format for slice notation is [start:stop:step]. The index method of a string finds the position of the first occurrence of its input.
Note however that if you are dealing with something more complex (such as matching patterns), you might want to look into Regular Expressions. For this operation though, the slice notation is sufficient.

Categories