Marc Brigham
Marc Brigham

Reputation: 2264

Need help splitting a string into 3 variables

I would like to split a string of characters into 3 variables. When I just run a .split() I get ['AAA00000011']. I would like this to be split like:

var1 = AAA
var2 = 0000001
var3 = 1

The user is typing in these values into the command line on windows machine.

My Code:

barcode = raw_input('Please enter your barcode')

print "this is the barcode all split up", barcode.split()

Upvotes: 1

Views: 145

Answers (3)

danielrsmith
danielrsmith

Reputation: 4060

I think you want to use substrings here.

var1 = barcode[:3]

etc.

Upvotes: 0

AlG
AlG

Reputation: 15157

Could also do it with an re (apologies, my re usage is rusty):

In [3]: re.findall('(\w{3})(\d{7})(\d)', 'AAA00000011')
Out[3]: [('AAA', '0000001', '1')]

Upvotes: 0

Björn Pollex
Björn Pollex

Reputation: 76778

Are you looking for this?

var1 = barcode[:3]   # first three characters
var2 = barcode[3:-1] # all characters from third to next-to-last
var3 = barcode[-1]   # last character

Upvotes: 6

Related Questions