python3+转义字符
时间: 2024-01-25 18:04:02 浏览: 138
Python代码-转义字符.py
在Python3中,转义字符的使用方式与Python2相同。可以使用反斜杠\来转义特殊字符。下面是一些常见的转义字符及其用法:
1. \n:换行符
```python
print("Hello\nWorld") # 输出:Hello
# World
```
2. \t:制表符
```python
print("Name:\tJohn") # 输出:Name: John
```
3. \r:回车符
```python
print("Hello\rWorld") # 输出:World
```
4. \\':单引号
```python
print('It\'s a sunny day') # 输出:It's a sunny day
```
5. \\":双引号
```python
print("She said, \"Hello!\"") # 输出:She said, "Hello!"
```
6. \\\:反斜杠
```python
print("C:\\Users\\John") # 输出:C:\Users\John
```
7. \b:退格符
```python
print("Hello\bWorld") # 输出:HellWorld
```
8. \f:换页符
```python
print("Hello\fWorld") # 输出:Hello
# World
```
9. \v:垂直制表符
```python
print("Hello\vWorld") # 输出:Hello
# World
```
10. \uXXXX:Unicode字符(XXXX为Unicode码)
```python
print("\u03A9") # 输出:Ω
```
11. \UXXXXXXXX:Unicode字符(XXXXXXXX为Unicode码)
```python
print("\U0001F600") # 输出:😀
```
阅读全文