我想将一串字符分成3个变量。当我运行.split()
时,我得到['AAA00000011']
。我希望将其拆分为:
var1 = AAA
var2 = 0000001
var3 = 1
用户在Windows机器的命令行中输入这些值。
我的代码:
barcode = raw_input('Please enter your barcode')
print "this is the barcode all split up", barcode.split()
答案 0 :(得分:6)
你在找这个吗?
var1 = barcode[:3] # first three characters
var2 = barcode[3:-1] # all characters from third to next-to-last
var3 = barcode[-1] # last character
答案 1 :(得分:0)
我想你想在这里使用子串。
var1 = barcode[:3]
等
答案 2 :(得分:0)
也可以用re(道歉,我的重复使用生锈):
In [3]: re.findall('(\w{3})(\d{7})(\d)', 'AAA00000011')
Out[3]: [('AAA', '0000001', '1')]