# 04-字符串「精卫」

AI悦创原创1v1Python 1v11v1Python 1v1大约 7 分钟...约 1956 字

## # 2. 探究单双三引号的作用以及区分

``````s = 'I'm zhaojinwei'
``````

File "/Users/huangjiabao/GitHub/SourceCode/MacBookPro16-Code/PythonCoder/StudentCoder/21-zhaojinyin/lesson01/Variable.py", line 1 s = 'I'm zhaojinwei' ^ SyntaxError: unterminated string literal (detected at line 1)

``````s = "I'm zhaojinwei"
print(s)
``````

``````I'm zhaojinwei
``````

``````s = """你好，我是悦创。

#1. 覆写 save 方法

print(s)
``````

``````你好，我是悦创。

#1. 覆写 save 方法

``````

## # 3. len() 检测字符串的长度

``````s = """你好，我是悦创。

#1. 覆写 save 方法

print(len(s))
``````
``````387
``````

## # 4. 字符串的字符获取

### # 4.1 提取单个字符

``````s = "aiyuechuang"
select = s[0]
print(select)

# ooutput
a
``````

### # 4.2 提取多个字符

``````s = "zhaojingwei"
select = s[0:4]
print(select)

# ooutput
zhao
``````

### # 4.3 提取不连续的多个字符

``````s = "zhaojingwei"
select = s[::2]
print(select)

# output
zajnwi
``````

## # 5. 字符串内置函数

### # 5.1capitalize() 字符串开头大写

``````s = "zhaojingwei"
new_s = s.capitalize()
print(new_s)

# --- output ---
Zhaojingwei
``````
``````s = "zhao jing wei"
new_s = s.capitalize()
print(new_s)

# --- output ---
Zhaojingwei
``````

### # 5.2 title()

``````s = "zhao jing wei"
new_s = s.title()
print(new_s)

# --- output ---
Zhao Jing Wei
``````

### # 5.3 upper()

``````s = "zhao jing wei"
new_s = s.upper()
print(new_s)

# --- output ---
ZHAO JING WEI
``````
``````s = "zhao jing wei"
new_s = s.upper()
print(new_s)
print(s)

# --- output ---
ZHAO JING WEI
zhao jing wei
``````

### # 5.4 lower()

``````s = "zhao jing wei"
new_s = s.upper().lower()
print(new_s)

s1 = "ZHAO JING WEI"
new_new_s = s1.lower()
print(new_new_s)

# --- output ---
zhao jing wei
zhao jing wei
``````

### # 5.5 find() 寻找元素下标「第一次出现的下标」找不到会返回 -1

``````s = "zhao jingz wei"
new_s = s.find("z")
print(new_s)

# --- output ---
0
``````

``````s = "zhao jingz wei"
new_s = s.find("p")
print(new_s)

# --- output ---
-1
``````
``````s = "zhao jingza wei"
new_s = s.find("za")  # 也可以寻找一个 word ，但是会返回 word 的第一个字母位置「下标」
print(new_s)

# --- output ---
9
``````

### # 5.6 index() 寻找元素下标「第一次出现的下标」找不到会报错

``````s = "zhao jingza wei"
new_s = s.index("z")
print(new_s)

# --- output ---
0
``````
``````s = "zhao jingza wei"
new_s = s.index("za")  # 也可以寻找一个 word ，但是会返回 word 的第一个字母
print(new_s)

# --- output ---
9
``````
``````s = "zhao jingza wei"
new_s = s.index("0")
print(new_s)

# --- output ---
Traceback (most recent call last):
File "/Users/huangjiabao/GitHub/SourceCode/MacBookPro16-Code/PythonCoder/StudentCoder/31-trj/01-demo.py", line 2, in <module>
new_s = s.index("0")
``````

### # 5.7 strip() 默认去掉前后空格

``````s = "  zhao jingza wei    "
print(s)
new_s = s.strip()  # 默认去掉前后空格
print(new_s)

# --- output ---
zhao jingza wei
zhao jingza wei
``````
``````s = "----  zhao jingza wei   ---"
print(s)
new_s = s.strip("-")  # 默认去掉前后空格
print(new_s)

# --- output ---
----  zhao jingza wei   ---
zhao jingza wei
``````
``````s = "-- --  zhao jingza wei   -- -"
print(s)
new_s = s.strip("-")  # 默认去掉前后空格
print(new_s)

# --- output ---
-- --  zhao jingza wei   -- -
--  zhao jingza wei   --
``````

### # 5.8 count() 字符计数

``````s = "-- --  zhao jingza wei   -- -"
new_s = s.count("z")
print(new_s)

# --- output ---
2
``````

``````s = "-- --  zhao jingzazh wei   -- -"
new_s = s.count("zh")
print(new_s)

# --- output ---
2
``````

``````s = "-- --  zhao jingzazh wei   -- -"
new_s = s.count("0")
print(new_s)

# --- output ---
0
``````

### # 5.9 startswith() 判断开始是不是特定字符

``````s = "zhao jingzazh wei"
new_s = s.startswith("z")
print(new_s)

# --- output ---
True
``````

``````s = "zhao jingzazh wei"
new_s = s.startswith("zhao")
print(new_s)

# --- output ---
True
``````

### # 5.10 endswith()

``````s = "zhao jingzazh wei"
new_s = s.endswith("zhao")
print(new_s)

# --- output ---
False
``````
``````s = "zhao jingzazh wei"
new_s = s.endswith("i")
print(new_s)

# --- output ---
True
``````
``````s = "zhao jingzazh wei"
new_s = s.endswith("ei")
print(new_s)

# --- output ---
True
``````

### # 5.11 rfind()

``````s = "zhao jingzazh wei"
new_s = s.rfind("z")  # right find
print(new_s)

# --- output ---
11
``````

### # 5.12 rstrip() or lstrip()

``````s = "----zhao jingzazh wei----"
new_s = s.lstrip("-")
print(new_s)

# --- output ---
zhao jingzazh wei----
``````
``````s = "----zhao jingzazh wei----"
new_s = s.rstrip("-")
print(new_s)

# --- output ---
----zhao jingzazh wei
``````

### # 5.13 isdigit() 判断字符串是不是纯数字

``````s = "zhaojingzazhwei"
new_s = s.isdigit()
print(new_s)

# --- output ---
False
``````
``````s = "1111111"
new_s = s.isdigit()
print(new_s)

# --- output ---
True
``````

### # 5.14 isalpha() 判断字符串是不是纯字母

``````s = "zhaojingwei"
new_s = s.isalpha()
print(new_s)

# --- output ---
True
``````

AI悦创·编程一对一

AI悦创·推出辅导班啦，包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然，还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线，随时响应！微信：Jiabcdefh

C++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

• 0
• 0
• 0
• 0
• 0
• 0

• 按正序
• 按倒序
• 按热度