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?
Related
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:
Remove final character from string
(5 answers)
Closed 4 years ago.
I'm trying to delete the last character of a string, and every documentation I can find says that this works.
string = 'test'
string[:-1]
print(string)
However, whenever I try it, my IDE tells me that line two has no effect, and when I run the code it outputs "test" and not "tes", which is what I want it to do. I think that the documentation I'm reading is about python 2 and not 3, because I don't understand why else this simple code wouldn't work. Can someone show me how to remove the last letter of a string in python 3?
new_string = string[:-1]
print(new_string)
You must save the string in the memory. When we assign a variable to the string without the last character, the variable then "stores" the new value. Thus we can print it out.
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.
This question already has answers here:
How do I verify that a string only contains letters, numbers, underscores and dashes? [duplicate]
(11 answers)
Closed 8 years ago.
I am trying to parse text and determine if it contains only letters and numbers, not special keyboard symbols like ! and #. I tried using .isalpha, but it says ! and # are valid. Is there away I can have something return false if it encounters one of these symbols?
Use regex matching:
import re
print re.match(r'^\w+$',your_string).group(0)
This matches the whole string only if it is alphanumeric
>>> print re.match(r'^\w+$', '1kjh2431k2j43').group(0)
'1kjh2431k2j43'
>>> print re.match(r'^\w+$', 'hjs7*Y##kha9Y*##').group(0)
NoneType
can check if the ordinals of the characters are in the ranges of characters, think the ranges are like 65-90ish and then 95-122ish, and then just check .isdigit() to see if the number was a digit beforehand
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. :)