sandeep
sandeep

Reputation: 3

striping string in python

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 ?

Upvotes: 0

Views: 271

Answers (1)

michel-slm
michel-slm

Reputation: 9756

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.

Upvotes: 2

Related Questions