用Python从某个位置复制字符串到另一个位置
我有一个字符串 test_string,内容是 'Hello, two is a number',我想复制从第7个字符到第12个字符之间的内容。在我使用的编程语言中:
test_string = 'Hello, two is a number'
new_string = string_copy(test_string, 7, 12)
新字符串 new_string 的值会是 ' two i'。我的例子可能有点傻,但在Python中有没有类似于 string_copy 的函数呢?
1 个回答
0
这是一个内置的功能,叫做 切片:
'Hello, two is a number'[7:12]
或者
test_string = 'Hello, two is a number'
new_string = test_string[7:12]
不过你似乎算错了,结果会是 'two i'(前面没有空格)。
补充:或者正如 Zero Piraeus 指出的,你的语言使用的是从1开始的索引,而不是像Python那样从0开始。
另外可以参考 这个链接