Python converting '\' to '\\'

后端 未结 5 1199
耶瑟儿~
耶瑟儿~ 2021-01-27 16:46

I am writing a program to sort a list fo input strings (song names). Those songnames contains latex chars like $\\lambda$, which i want to get sorted like \'lambda\' instead, s

5条回答
  •  Happy的楠姐
    2021-01-27 17:14

    There is nothing wrong with the code: what you're experiencing is repr behaviour. Printing a list causes it's contents to be repr'd, and repr escapes \.

    print repr(r'\foo') # => '\\foo'
    

    If you want to print the list without repr, you use either a loop or str.join.

提交回复
热议问题