Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中按地址分割字符串,就像在C中一样(Python的字符串切片)_Python_C_Python 2.7_Pointers_Slice - Fatal编程技术网

在Python中按地址分割字符串,就像在C中一样(Python的字符串切片)

在Python中按地址分割字符串,就像在C中一样(Python的字符串切片),python,c,python-2.7,pointers,slice,Python,C,Python 2.7,Pointers,Slice,在C语言中,您可以通过以下操作访问字符串中您想要的位置,地址为char: &string[index] 例如,此代码: #include <stdio.h> int main() { char *foo = "abcdefgh"; printf("%s\n", &foo[2]); } 在Python中有这样做的方法吗 这就是你可以做到的: foo = "abcdefgh" print foo[2:] 更一般地说foo[a:b]表示从位置a(包括)到b(

在C语言中,您可以通过以下操作访问字符串中您想要的位置,地址为char:

&string[index]
例如,此代码:

#include <stdio.h>

int main()
{
  char *foo = "abcdefgh";
  printf("%s\n", &foo[2]);
}

在Python中有这样做的方法吗

这就是你可以做到的:

foo = "abcdefgh"
print foo[2:]

更一般地说
foo[a:b]
表示从位置
a
(包括)到
b
(不包括)的字符。

这是您可以做到的:

foo = "abcdefgh"
print foo[2:]
更一般地说
foo[a:b]
表示从位置
a
(包括)到
b
(不包括)的字符。

在Python中称为字符串切片,语法为:

>>> foo = "abcdefgh"
>>> foo[2:]
'cdefgh'
检查哪个选项演示了切片功能以及python中字符串可用的其他函数

我还建议看一看:在哪里用一些非常好的例子演示了它

以下是与字符串切片相关的几个示例:

>>> foo[2:]     # start from 2nd index till end
'cdefgh'
>>> foo[:3]     # from start to 3rd index (excluding 3rd index)
'abc'
>>> foo[2:4]    # start from 2nd index till 4th index (excluding 4th index)
'cd'
>>> foo[2:-1]   # start for 2nd index excluding last index
'cdefg'
>>> foo[-3:-1]  # from 3rd last index to last index ( excluding last index)
'fg'
>>> foo[1:6:2]  # from 1st to 6th index (excluding 6th index) with jump/step of "2"
'bdf'
>>> foo[::-1]   # reverse the string; my favorite ;)
'hgfedcba'
在Python中称为字符串切片,语法为:

>>> foo = "abcdefgh"
>>> foo[2:]
'cdefgh'
检查哪个选项演示了切片功能以及python中字符串可用的其他函数

我还建议看一看:在哪里用一些非常好的例子演示了它

以下是与字符串切片相关的几个示例:

>>> foo[2:]     # start from 2nd index till end
'cdefgh'
>>> foo[:3]     # from start to 3rd index (excluding 3rd index)
'abc'
>>> foo[2:4]    # start from 2nd index till 4th index (excluding 4th index)
'cd'
>>> foo[2:-1]   # start for 2nd index excluding last index
'cdefg'
>>> foo[-3:-1]  # from 3rd last index to last index ( excluding last index)
'fg'
>>> foo[1:6:2]  # from 1st to 6th index (excluding 6th index) with jump/step of "2"
'bdf'
>>> foo[::-1]   # reverse the string; my favorite ;)
'hgfedcba'
对你来说,“切片”就是答案

语法:
s[a:b]

这将为您提供从索引a到b-1的字符串 如果您希望字符串从索引开始直到结束,请使用

s[a:][/code>

如果您希望字符串从开始到索引b,那么使用

s[:b+1]

举个例子:

s="abcdefgh"
print s[2:]
将打印
cdefgh
,这就是问题的答案

您可以从

中了解更多信息。问题的答案是“切片”

语法:
s[a:b]

这将为您提供从索引a到b-1的字符串 如果您希望字符串从索引开始直到结束,请使用

s[a:][/code>

如果您希望字符串从开始到索引b,那么使用

s[:b+1]

举个例子:

s="abcdefgh"
print s[2:]
将打印
cdefgh
,这就是问题的答案


你可以从

中了解更多,这个答案没有错,但可以改进。你能给提问者更多的信息,让他们更好地学习吗?使用[:]时它叫什么?你能给他们指出一个说明如何使用它的参考资料,并在你的答案中总结这个参考资料吗?这个答案没有错,但可以改进。你能给提问者更多的信息,让他们更好地学习吗?使用[:]时它叫什么?你能给他们指出一个说明如何使用它的参考资料,并在你的回答中总结这个参考资料吗?