简体   繁体   English

Python中的“ C风格”字符串

[英]'C-style' strings in Python

A very popular question is how to reverse a C-Style string. 一个非常普遍的问题是如何反转C样式字符串。 The C-Style string by definition is a string that is terminated by a null('\\0'). 根据定义,C样式字符串是一个以null('\\ 0')结尾的字符串。 Using C(or maybe C++), it is possible to use pointers to manipulate the string to reverse its contents in-place. 使用C(或C ++),可以使用指针来操纵字符串以就地反转其内容。

If somebody asks the question, "How do you reverse a C-style string in Python?", what would some of the possible answers be? 如果有人问“如何在Python中反转C样式的字符串?”,那么可能的答案是什么?

Thanks 谢谢

If you need to "reverse a C-style string in Python", I think the end result must also be a c-style string. 如果您需要“在Python中反转C样式的字符串”,我认为最终结果也必须是C样式的字符串。

That is how I would understand the question, but the above answers do not support this. 这就是我要理解的问题,但是以上答案并不支持这一点。

See the interactive session below: 请参阅下面的互动式会议:

>>> 
>>> original = "abc\0"
>>> finish_correct = "cba\0"
>>> original
'abc\x00'
>>> finish_correct
'cba\x00'
>>> 
>>> answer = original[:-1]  # remove final null
>>> answer = answer[::-1]  # reverse string
>>> # Extended slice syntax:  [begin:end:step]
>>> # So, [::-1] means take whole string, but in reverse.
>>> answer
'cba'
>>> answer = answer + "\0"
>>> answer
'cba\x00'
>>> answer == finish_correct
True

Also note, Python strings are immutable. 还要注意,Python字符串是不可变的。 This means that they can never be changed. 这意味着它们永远无法更改。 You can make new strings that are assigned to the same variable name, but the in-memory image for a given strings will never change. 您可以创建分配给相同变量名称的新字符串,但是给定字符串的内存图像将永远不会改变。 Thus, the notion of "reverse a string in place" can not happen in Python. 因此,“在原处反转字符串”的概念在Python中不会发生。

Hope this helps. 希望这可以帮助。 If so, please up-vote and accept the answer. 如果是这样,请投票并接受答案。 Thanks. 谢谢。 :-) :-)

Python doesn't use C-style strings; Python不使用C样式的字符串。 Python strings can contain an embedded NUL, so C-style strings are not used, but rather an explicit length. Python字符串可以包含嵌入式NUL,因此不使用C样式的字符串,而是使用显式的长度。

>>> 'abc\0def'[::-1]
'fed\x00cba'

Since C doesn't have a string type, it represents character strings as pointers to char , where the last byte (assuming ASCII, not wide-characters) is \\0 . 由于C没有字符串类型,因此它将字符串表示为char指针,其中最后一个字节(假定为ASCII,不是宽字符)为\\0 It's a representation . 这是一种表示 By the way, this default implementation has the deficiency that \\0 can't be part of such a string. 顺便说一下,此默认实现的缺点是\\0不能是这样的字符串的一部分。 If that's needed, a different representation is required (such as representing a string as a pointer + a length integer). 如果需要,则需要使用其他表示形式(例如,将字符串表示为指针+长度整数)。

Python, OTOH has a string type, and it's opaque to the user how this type is represented. Python,OTOH具有字符串类型,并且对于用户而言,如何表示这种类型是不透明的。 Therefore, a "C-style string" is a meaningless concept in Python. 因此,“ C样式字符串”在Python中是没有意义的概念。

Python strings are immutable . Python字符串是不可变的 You could emulate a C-style string with a table of chars, but I don't see why you would bother. 您可以使用一个字符表来模拟C样式的字符串,但是我不明白您为什么要打扰。

But if you did have a C-style "string" (ie table of characters), then all you'd need to do is swap s[i] with s[len(s)-i-1]: 但是,如果您确实有C风格的“字符串”(即字符表),那么您要做的就是将s [i]与s [len(s)-i-1]交换:

for i in range(0, len(a) - 2):
    a[i], a[len(a) - 1 - i] = a[len(a) - 1 - i], a[i]

(if a is your C-style string) (如果a是您的C样式字符串)

Note how you don't need a temporary variable(granted you don't need one in C either, considering how you could use the null character as a temp space). 请注意,您不需要临时变量(考虑到如何将空字符用作临时空间,也可以在C中不需要临时变量)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM