Skip to main content
 首页 » 编程设计

python之需要帮助将字符串拆分为 3 个变量

2025年05月04日30yxwkf

我想将一串字符拆分为 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() 

请您参考如下方法:

你在找这个吗?

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