简体   繁体   English

如何将普通字符串转换为 Python 中的转义字符?

[英]How to convert a normal string to escape characters in Python?

I want to convert string with all escape character.我想用所有转义字符转换字符串。

Input输入

string = "Let's do this"
convert(string)

Output Output

Let\'s do this

Thank You!谢谢你!

Your question is not clear.你的问题不清楚。 By escape character do you mean adding new line after certain characters or just want to add backslash do your string?转义字符是指在某些字符之后添加新行还是只想在字符串中添加反斜杠?

if you want to add backslash before ' character you can try below.如果您想在 ' 字符之前添加反斜杠,您可以在下面尝试。

new_string = string.replace("'s", "\\'s")

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

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