# Grabbing 1 Value From a Long Strin

So I am reading in a value from a scale head that is set to continuously output. Right now I have a button that says when to grab a value from the scale. The only problem is that it gives me a long list of what appears to be every value since the last time I pressed the button. For example, I would get something like:

17.9 lbG
18.6 lbG
19.2 lbG
19.2 lbG
19.2 lbG
And this could go on for 20 or so more values.

The scripting I have on my button is as follows:
print weight

Iām wondering whatās the best way to go about grabbing the very last value? I would use a .split() script but it seems that wouldnāt work if I donāt know how many values there will be. I was thinking about adding all the values to a list and then using something like list[-1]. Does this sound plausible or is there an easier way of going about it?

Split() creates a list, so

``````lastVal = weight.split("\n")[-1]
``````

should work fine.

2 Likes

In python you can get the last few characters from a string using this syntax:

``````mystring = "i want to get only the last part"
substring = mystring[-4:]
print substring
#output > "part"``````

Good to know. My only problem here would be when a weight goes from, say, 99 lbs to 105 lbs. It would leave off the ā1ā correct?

Ah right, So @mrogers 's solution is better

Thank you for the quick response. So where am I going wrong here? This is what Iāve got and Iām just getting a blank output.

finalWeight = weight.split(āGā)[-1]

print finalWeight

Edit: i figured it out. The last line should be
print finalWeight[-1]

Is it possible `weight` is blank?