轉義字符
- 什么是轉義字符
- 轉義字符就是反斜杠+想要實現的轉義功能字母
- 為什么要進行轉義字符
- 當字符串中包括反斜杠,單引號加雙引號等有特殊用途的字符時,必須使用反斜杠對這些字符進行轉義(轉換一個含義)
- 反斜杠:\\
- 單引號:\'
- 雙引號:\"
- 當宇符串中包含換行、回車,水平制表符或退格等無法直接表示的特殊字符時,也可以使用轉義字符當宇符串中包含換行、回車,水平制表符或退格等無法直接表示的特殊字符時,也可以使用轉義字符
- 換行:\n
- 回的:\r
- 水平制表符:\t
- 退格:\b
-
print("hello\nword")#換行字符 print("hello\tworld") print("helloooo\tworld") print("hello\rworld")#world將hello進行了覆蓋 print("hello\bworld")#\b是退格的意思,將o退掉 print('http:\\\\http://www.baidu.com/') print('我說:\'天氣真好\'')
-
運行結果:
-
-
原字符,不希望字符串中的轉義字符起作用,就是用原字符,就是在字符串前加上r,或R
-
#原字符,不希望字符串中的轉義字符起作用,就是用原字符,就是在字符串前加上r,或R print(r"hello\nworld") #注意事項,最后一個字符不能是反斜杠 #print(r"hello\nworld\") print(r"hello\nworld\\")
運行結果: