This question already has answers here:
How to get the position of a character in Python?
                                
                                    (9个答案)
                                
                        
                                4年前关闭。
            
                    
例如:

   my_string = "hi how are you?/nIs everything ok?/nAre you happy?"


我需要制作一个包含换行符-(/ n)的所有索引的列表。
我该怎么做 ?

最佳答案

您可以在列表推导中使用enumerate创建索引列表。

>>> [index for index, value in enumerate(my_string) if value == '\n']
[15, 33]


顺便说一句,换行符是'\n'而不是'/n'(请注意斜杠)

10-07 17:58