2014-07-05 70 views
0

我有一個字符串test_string像「你好,二是一些」」,我想第7位和12之間的字符複製在我使用的編程語言:蟒蛇複製串定位

test_string = 'Hello, two is a number' 
new_string = string_copy(test_string, 7, 12) 

new_string的值將是「兩我」。我的例子可能有點愚蠢,但在Python不函數存在等於string_copy?

回答

0

這是一個內置的功能,這就是所謂的切片

'Hello, two is a number'[7:12] 

test_string = 'Hello, two is a number' 
new_string = test_string[7:12] 

你看起來雖然已經算錯,因爲其結果將是「兩個I」(沒有前導空格)。

編輯:或者,如零比雷埃夫斯指出,您的語言使用1 - 基於索引的對比到Python

又見https://docs.python.org/3/tutorial/introduction.html#lists

+0

這應該是'「你好,二是一些」 [ 6:12]'...問題假設基於1的索引和關閉[間隔](http://en.wikipedia.org/wiki/Interval_%28mathematics%29#Terminology)(這是合理的,只是不是Python如何做它),而Python的切片是[基於零](http://en.wikipedia.org/wiki/Zero-based_numbering)和半開放。 –

+1

感謝您的答案,您的方法就像一個魅力。我檢查了確實使用基於1的索引的GML(Game Maker Language)文檔。 –