2014-01-20 57 views
1

這是我試圖運行代碼:如何將元組轉換爲int?

line = "123456789" 
p = 2 
print line[p,p+2] 

而我得到的錯誤 - TypeError: string indices must be integers, not tuple。我如何在變量中使用line [,]。任何幫助表示讚賞。

+9

你可能是指'線[P:P + 2]'。 – RemcoGerlich

回答

2

您想使用冒號進行切片。

line = "123456789" 
p = 2 
print line[p:p+2] 

工作正常。

輸出:

34 
1
line = "123456789" 
p = 2 
print line[p,p+2] # this is incorrect slice notation 

正確的形式是:

print line[p:p+2] # with a colon 

here有關字符串的信息和字符串切片