转义字符是通常用于执行某些任务的字符,它们在代码中的使用指示编译器采取映射到该字符的适当操作。
null
例子:
'' --> Leaves a line ' ' --> Leaves a space
# Python code to demonstrate escape character # string ch = "ILove Geeksforgeeks" print ( "The string after resolving escape character is : " ) print (ch) |
输出:
The string after resolving escape character is : I Love Geeksforgeeks
但在某些情况下,不希望解析转义,即整个未解析字符串 待打印。这些都是通过以下方式实现的。
使用repr()
此函数以可打印格式返回字符串,即不解析转义序列。
# Python code to demonstrate printing # escape characters from repr() # initializing target string ch = "ILove Geeksforgeeks" print ( "The string without repr() is : " ) print (ch) print ( " ) print ( "The string after using repr() is : " ) print ( repr (ch)) |
输出:
The string without repr() is : I Love Geeksforgeeks The string after using repr() is : 'ILove Geeksforgeeks'
使用“r/r”
将“r”或“r”添加到目标字符串会在内部触发字符串的repr(),并停止转义字符的解析。
# Python code to demonstrate printing # escape characters from "r" or "R" # initializing target string ch = "ILove Geeksforgeeks" print ( "The string without r / R is : " ) print (ch) print ( " ) # using "r" to prevent resolution ch1 = r "ILove Geeksforgeeks" print ( "The string after using r is : " ) print (ch1) print ( " ) # using "R" to prevent resolution ch2 = R "ILove Geeksforgeeks" print ( "The string after using R is : " ) print (ch2) |
输出:
The string without r/R is : I Love Geeksforgeeks The string after using r is : ILove Geeksforgeeks The string after using R is : ILove Geeksforgeeks
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END