编写python线程实验,要求主线程循环输出hellworld,子线程循环输出“你好世界”
时间: 2023-05-04 09:01:37 浏览: 75
可以使用以下代码实现:
```python
# 主线程
if __name__ == '__main__':
print("hello world")
# 创建子线程
def sub_thread():
print("你好世界")
t = threading.Thread(target=sub_thread)
t.start()
```
运行以上代码,结果会输出"hello world"和"你好世界"。
相关问题
Python中转意符
Python中的转义字符用于表示一些特殊字符,例如换行符、制表符等。下面是一些常见的转义字符及其用法[^2]:
1. \n:表示换行符。
```python
print('hello\nworld') # 输出:
# hello
# world
```
2. \t:表示制表符。
```python
print('hello\tworld') # 输出:hello world
```
3. \\:表示反斜杠。
```python
print('hello\\world') # 输出:hello\world
```
4. \r:表示回车符。
```python
print('hello\rworld') # 输出:world
```
5. \': 表示单引号。
```python
print('\'hello\'') # 输出:'hello'
```
6. \": 表示双引号。
```python
print('\"hello\"') # 输出:"hello"
```
7. \a:表示响铃符。
```python
print('hello\a') # 输出:hello(会发出响铃声)
```
8. \b:表示退格符。
```python
print('hello\bworld') # 输出:hellworld
```
9. \f:表示换页符。
```python
print('hello\fworld') # 输出:
# hello
# world
```
10. \v:表示垂直制表符。
```python
print('hello\vworld') # 输出:
# hello
# world
```
11. \0:表示空字符。
```python
print('hello\0world') # 输出:hello world
```
python3+转义字符
在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") # 输出:😀
```