Add Day_3: 字符串
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
# Day_3 · 字符串:和文字打交道
|
||||
|
||||
> 预计耗时:20~30 分钟
|
||||
> 后端天天处理文字(用户名、日志、网页内容)。今天把字符串的基本操作学扎实。
|
||||
|
||||
## 今日知识点
|
||||
|
||||
- 字符串拼接:`+`(如 `"你" + "好"`)
|
||||
- **f-string**(推荐):`f"你好,{name}!"` —— 大括号里直接放变量
|
||||
- 常用方法:`len(s)` 长度、`s.upper()` 大写、`s.lower()` 小写、`s.strip()` 去首尾空格、`s.split(" ")` 按空格切分
|
||||
|
||||
## 先学(教程)
|
||||
|
||||
- 菜鸟教程("字符串"一节,重点看 f-string 和常用方法):https://www.runoob.com/python3/python3-string.html
|
||||
- B站黑马教程(看讲"字符串"的几集):https://www.bilibili.com/video/BV1qW4y1a7fU/
|
||||
|
||||
## 任务(后做)
|
||||
|
||||
1. 用 f-string 写自我介绍:
|
||||
```python
|
||||
name = "张三"
|
||||
age = 18
|
||||
city = "青岛"
|
||||
print(f"我叫{name},今年{age}岁,来自{city}。")
|
||||
```
|
||||
把名字换成你自己的,运行确认。
|
||||
2. 字符串方法练习,**先猜输出再运行**:
|
||||
```python
|
||||
s = "Hello World"
|
||||
print(len(s)) # 长度是多少?
|
||||
print(s.upper()) # 大写
|
||||
print(s.lower()) # 小写
|
||||
print(s.strip()) # 首尾没空格也能跑,试试 " Hello ".strip()
|
||||
```
|
||||
3. **进阶(加分)**:`words = "apple banana cherry".split(" ")`,
|
||||
打印切出来的列表,再数一数有几个单词(用 `len(words)`)。
|
||||
|
||||
## 完成标准
|
||||
|
||||
- [ ] 任务 1 打印出完整自我介绍
|
||||
- [ ] 任务 2 四行输出都符合预期
|
||||
- [ ] (进阶)能切分字符串并数出单词个数
|
||||
|
||||
**提交方式**:把 `day3.py` 代码贴在当日工单评论里。
|
||||
|
||||
## 参考解答(先做再看)
|
||||
|
||||
```python
|
||||
name = "张三"
|
||||
age = 18
|
||||
city = "青岛"
|
||||
print(f"我叫{name},今年{age}岁,来自{city}。")
|
||||
|
||||
s = "Hello World"
|
||||
print(len(s)) # 11(含空格)
|
||||
print(s.upper()) # HELLO WORLD
|
||||
print(s.lower()) # hello world
|
||||
print(" Hi ".strip()) # Hi
|
||||
|
||||
# 进阶
|
||||
words = "apple banana cherry".split(" ")
|
||||
print(words) # ['apple', 'banana', 'cherry']
|
||||
print(len(words)) # 3
|
||||
```
|
||||
Reference in New Issue
Block a user