-1

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?

apbassett
  • 160
  • 1
  • 6
  • 14

2 Answers2

6

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.

Mathias711
  • 6,568
  • 4
  • 41
  • 58
4

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?

Andy
  • 49,085
  • 60
  • 166
  • 233