0

Better way of striping a string I would like to extract the number from a string of the type:

Var = '0x456;//used for fooing'  

##I do it this way but I am sure there is much better way of doing this :
v = Var.split(';')  
c= v[0]  

##use c for further processing ....  

What is the better way of extracting the substring 0x456 from the given Var ?

3
  • Your solution is the best for read. Commented Mar 30, 2011 at 9:32
  • "Striping"? Perhaps you mean "parsing"? It can't be "stripping" because "strip()" is already a string method and doesn't seem to be what you're talking about. Commented Mar 30, 2011 at 9:54
  • @aix By better I mean more pythonic style . @S.Lott yes you are right I meant parsing not striping which can mean the specific python method. I should be more clearer and precise. Commented Mar 30, 2011 at 10:07

1 Answer 1

2

There is a more efficient way, as you conjectured; since you only need the first token, you can just find the index of the ';' character and take the substring before that.

Var[:Var.index(';')]

ps it's not the Python naming convention to upper-case the first letter of a variable name; that's conventionally used for classes.

Sign up to request clarification or add additional context in comments.

5 Comments

This is less pythonic compared to my comment above. Its unnecessary slicing and achieves nothing that isn't achieved by the .split function call
That's not necessarily correct. Python strings are immutable, so slicing and .split both have to construct new strings. The difference is that my slicing, and the one in the other answer, both only construct one new string. Your solution constructs at least two new strings (depending on whether the C++-style comment also contains ;) and a list to contain them. On the other hand, it's true that using .index or .find requires traversing the input variable twice -- once to find the index and another to slice it. But since the number being parsed is of limited length this is negligible.
Personally using the .split method is preferable surely? as they can then take both sides without re slicing.
only if they want to take both sides, so it depends on usage. plus, as I said, the comment might include ; so it's not really both sides -- you might end up with multiple splits. And in your solution you don't have access to the other splits anyway since you do not store a reference to the list of splits. Now, if Python has a good optimizing compiler it might notice that you're discarding the splits anyway and only compute the first one, but then it will just generate the same bytecode as the two non-split solutions.
It just seems like your method is alittle more unpythonic... Sorry 'n' all. But I can see your points.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.