当前位置:首页>python>python笔记系列之——字符串

python笔记系列之——字符串

  • 2026-04-19 07:23:45
python笔记系列之——字符串

小朋友们,你们好!欢迎来到Python魔法课堂。今天,我们要认识一个超级重要的魔法材料——字符串。字符串是什么?简单来说,它就是“一串字符”,就像把一个个字母、数字、汉字串起来的项链。你可以用它来存储名字、写下故事、甚至和电脑聊天!

在Python的世界里,字符串可是无处不在。掌握了字符串的魔法,你就能轻松地处理文字,让程序变得更加有趣。准备好了吗?让我们开始今天的冒险!


一、字符串的定义——给文字穿上“引号外衣”

字符串就是用引号括起来的一串字符。Python里可以用单引号双引号三个单引号三个双引号来定义。

city = '北京'          # 单引号name = "小明"          # 双引号sentence = '''我今年8岁了'''  # 三引号(可以换行)sentence = """我喜欢学习Python"""  # 三引号(可以换行)

为什么要有不同种类的引号?因为有时候字符串里面本身包含引号,用不同的引号可以避免麻烦。

talk = "他说:'你好!'"      # 外面双引,里面单引,没问题talk2 = '他说:"你好!"'     # 外面单引,里面双引,也没问题

如果非要混用同一种引号,就需要用到转义字符 \" 或 \',后面会讲到。

试试看:定义一个字符串,内容是 I'm a student(包含单引号),看看怎么实现。


二、字符串的拼接和重复——像玩积木一样连接文字

1. 加法 +:把两个字符串粘在一起

first = "Hello"last = "World"greeting = first + " " + lastprint(greeting)  # 输出 "Hello World"

就像把两块积木拼在一起。注意:字符串只能和字符串相加,不能和数字直接相加。

# 错误示例print("abc" + 123)   # 会报错!# 正确做法:把数字转成字符串print("abc" + str(123))  # 输出 "abc123"

2. 乘法 *:重复字符串

laugh = "哈" * 5print(laugh)  # 输出 "哈哈哈哈哈"

想打印一条长长的分隔线?这样就行:

print("-" * 20)  # 输出 --------------------

试试看

  1. 用加法拼接 "Python" 和 "魔法",中间加一个空格。

  2. 用乘法打印一条由 "*" 组成的10个星星的线。

  3. 用 "😀" * 3 打印三个笑脸。


三、索引——给每个字符编号

字符串里的每个字符都有自己的“位置编号”,就像电影院里的座位号一样,每个字符都有自己的号码。我们把这个“位置”叫做索引(也叫下标),第一个字符从0开始数

1. 正索引:从左往右数

Python里,字符串的第一个字符编号是 0,第二个是 1,第三个是 2……以此类推。

比如字符串 "Python"

字符
P
y
t
h
o
n
正索引
0
1
2
3
4
5
s = "Python"print(s[0])  # 输出 Pprint(s[1])  # 输出 yprint(s[5])  # 输出 n

小练习:字符串 "Hello" 中,位置2是什么字符?位置4呢?自己试试看!

2. 负索引:从右往左数

除了从左往右数,Python还允许我们从右往左数!最后一个字符是 -1,倒数第二个是 -2,倒数第三个是 -3……

还是字符串 "Python"

字符
P
y
t
h
o
n
正索引
0
1
2
3
4
5
负索引
-6
-5
-4
-3
-2
-1
s = "Python"print(s[-1])  # 输出 n(最后一个)print(s[-2])  # 输出 o(倒数第二个)print(s[-6])  # 输出 P(第一个)

小练习:字符串 "abcdefg" 中,-3 位置是什么?-7 呢?用Python验证一下。


四、切片——像切面包一样切字符串

切片就是从字符串里切出一段来。就像从长长的法棍面包上切下一片,你可以指定从哪里开始切,到哪里结束。

切片的语法是:[起始:结束]

重要规则

  • 包含起始位置的字符

  • 不包含结束位置的字符(就像“到那个位置之前停止”)

举个例子,"Hello, World!"[0:5] 会切出索引0到4的字符,也就是 "Hello"

s = "Hello, World!"print(s[0:5])   # 输出 Helloprint(s[7:12])  # 输出 World

记忆小窍门[起始:结束] 就像“从起始开始,走到结束之前停下”。

1. 只写起始,不写结束 —— 从起始一直切到末尾

如果你只写起始位置,不写结束位置,Python会从起始一直切到字符串的最后一个字符。(原理是:如果不输入终止序号,Python执行时就不知道该拿后面多少个字符,就会把起始字符之后的所有字符都放在分片中,反过来也一样)

s = "Hello, World!"print(s[7:])   # 输出 World!(从索引7到末尾)print(s[0:])   # 输出 Hello, World!(整个字符串)

2. 只写结束,不写起始 —— 从开头切到结束之前

如果你只写结束位置,不写起始位置,Python会从开头(索引0)一直切到结束位置之前。

s = "Hello, World!"print(s[:5])   # 输出 Hello(从开头到索引4print(s[:7])   # 输出 Hello, (从开头到索引6

3. 起始和结束都不写 —— 复制整个字符串

如果你什么都不写,[:] 会复制整个字符串。

s = "Hello, World!"t = s[:]print(t)       # 输出 Hello, World!

4. 用负索引切片

负索引也可以用在切片里!非常方便。

s = "Hello, World!"print(s[-6:-1])   # 输出 World(-6是W,-1是!,但不包含-1print(s[-6:])     # 输出 World!(从-6到末尾)print(s[:-1])     # 输出 Hello, World(去掉最后一个字符)

小练习

  1. 从字符串 "abcdefghij" 中切出 "def"

  2. 从字符串 "Python is fun" 中切出 "is"

  3. 从字符串 "1234567890" 中切出最后4个数字(用负索引)。


五、步长 —— 跳着切

有时候我们不想连续地切,而是想每跳几步取一个。这时候就要用到 步长

语法变成:[起始:结束:步长]

步长的意思是:每次获取完字符之后,走几步才能获取下一个字符。(不填步长的话,步长默认为1)

1. 步长为2 —— 走两步,取一个

s = "Hello, World!"print(s[::2])   # 输出 Hlo ol!(每隔一个取一个)

解释:取索引0(H)、2(l)、4(o)、6(空格)、8(o)、10(l)、12(!)。

2. 步长为3 —— 走三步,取一个

s = "Hello, World!"print(s[::3])   # 输出 HlWrd(取0,3,6,9,12

3. 步长为负数 —— 倒着切!

这是切片最酷的功能之一!步长可以是负数,这样就会从右往左切。

s = "Hello"print(s[::-1])  # 输出 olleH(反转字符串)

解释:步长-1表示每次向左走一步,从最后一个字符开始,一直走到第一个。

4. 组合起始、结束和步长

你可以把起始、结束、步长任意组合,实现各种效果。

s = "abcdefghij"print(s[1:8:2])   # 输出 bdfh(索引1,3,5,7print(s[:8:2])    # 输出 aceg(索引0,2,4,6print(s[1::2])    # 输出 bdfhj(索引1,3,5,7,9

5. 负步长时,起始和结束要反过来

当步长为负数时,切片是从右向左走。所以起始应该大于结束,否则会切出空字符串。

s = "abcdefghij"print(s[8:3:-1])   # 输出 ihgfe(从索引8开始,向左走到索引4

如果不理解,可以这样记:步长为正时从左往右,起始<结束;步长为负时从右往左,起始>结束。

小练习

  1. 从字符串 "0123456789" 中切出 "86420"(偶数位倒序)。

  2. 从字符串 "Hello, World!" 中切出 "dlroW"(注意逗号和空格)。

  3. 用一步切片反转字符串 "Python"


六、切片常见错误和注意事项

1. 索引超出范围怎么办?

如果切片时起始或结束索引超出了字符串的长度,Python不会报错,而是自动截取到边界。

s = "Hello"print(s[0:100])   # 输出 Hello(超出部分忽略)print(s[100:])    # 输出空字符串

2. 起始大于结束,且步长为正

如果起始 > 结束,且步长为正,会得到空字符串。

s = "Hello"print(s[3:1])    # 输出空字符串(因为从左往右走,但31右边)

3. 切片不会修改原字符串

记住:字符串是不可变的,切片返回的是新的字符串,原字符串不变。

s = "Hello"t = s[1:3]    # t = "el"print(s)      # 仍然是 "Hello"

七、字符串的不可变性和id

1. 什么是不可变性?

字符串有一个非常重要的特性:不可变(immutable)。意思是,一旦你创建了一个字符串,你就不能改变它里面的任何一个字符。你只能创建一个新的字符串。

s = "Hello"# s[0] = "h"  # 如果去掉注释,会报错!

为什么不能改?因为Python在设计时认为字符串应该是只读的。这样做有很多好处:

  • 安全:字符串作为字典的键、或者传递到函数中时,不用担心被意外修改。

  • 高效:Python可以复用相同的字符串,节省内存。

  • 简单:不用考虑字符串变化带来的复杂情况。

如果你想得到一个修改后的字符串,只能新建一个:

s = "Hello"new_s = "h" + s[1:]   # 新建一个字符串 "hello"print(new_s)          # 输出 helloprint(s)              # 原来的 s 还是 "Hello",没变

2. 什么是id?—— 字符串的“身份证号”

每个对象(包括字符串)在计算机内存里都有一个独一无二的“地址”(还记得变量的本质吗?)。id() 函数可以返回这个地址(一个整数)。你可以把它想象成字符串的身份证号

s1 = "Hello"s2 = "Hello"print(id(s1))   # 例如 140735310356784print(id(s2))   # 可能和 s1 一样!

如果两个字符串的 id 相同,说明它们在内存中是同一个对象。Python会为了节省内存,自动把内容相同的一些短字符串重用。这叫字符串驻留(String Interning)

3. 什么时候id相同?什么时候不同?

相同的情况:通常对于只包含字母、数字、下划线的短字符串,Python会复用。

a = "hello"b = "hello"print(a is b)   # True,a和b指向同一个对象

不同的情况:如果字符串很长,或者包含空格、标点等,Python可能不会复用。

c = "hello world"d = "hello world"print(c is d)   # 可能是 False,不保证复用

特别注意:用 + 拼接得到的字符串,即使内容和另一个相同,id 也可能不同。

e = "Hello"f = "He" + "llo"print(e == f)   # True,内容一样print(e is f)   # 可能是 False,因为 f 是新建的

4. 不可变性对id的影响

因为字符串不可变,所以如果你对字符串做任何“修改”操作(比如 upper()replace() 等),都会产生一个新的字符串,原来的字符串保持不变。

s = "python"t = s.upper()print(s)      # "python" 没变print(t)      # "PYTHON" 是新字符串print(id(s))  # 某个地址print(id(t))  # 不同的地址

5. 小练习

  1. 创建两个字符串 "cat" 和 "cat",用 id() 看看它们是否相同。

  2. 创建字符串 "a" * 1000 和 "a" * 1000,比较它们的 id(通常不同,因为太长了)。

  3. 用 += 拼接字符串多次,观察每次是否创建新对象。


八、字符串比大小——谁更大?

字符串之间可以用比较运算符(><== 等)比较大小。它比较的是字符的Unicode编码顺序。简单来说,就是按照字母表的顺序:

  • 小写字母 a 到 z 依次增大

  • 大写字母 A 到 Z 依次增大,但大写字母的编码比小写字母小('A' < 'a'

  • 数字 '0' 到 '9' 的编码依次增大,且比大写字母小

print("apple" < "banana")   # True,因为'a'<'b'print("Apple" < "apple")    # True,因为'A'<'a'print("123" > "89")         # False,因为先比较第一个字符'1'<'8'

比较是从第一个字符开始,挨个比较,如果分出大小,不管后面还有多少没比,都不比了。如果一个字符串是另一个的前缀,短的更小。

print("abc" < "abcd")   # True,因为"abc"是"abcd"的前缀

试试看

  1. 判断 "Python" 和 "python" 哪个大。

  2. 判断 "123" 和 "12" 哪个大。

  3. 给几个单词排序:"cat""dog""apple",用<比较。


九、检查是否包含——in和not in

你想知道一个字符串里有没有某个子串?用 in 或 not in

s = "Hello, World!"print("World" in s)     # Trueprint("world" in s)     # False,python中严格区分大小写print("Python" in s)    # Falseprint("Hi" not in s)    # True

这个非常有用,比如检查一句话里有没有“苹果”。

试试看

  1. 输入一句话,判断里面是否包含“Python”。

  2. 判断一个文件名是否以 .py 结尾(可以用endswith,但这里用in也可以,不过in会匹配中间)。


十、格式化字符串——让文字更灵活

有时候你想把变量插入到字符串中,就像填空一样。Python提供了好几种方法。我们先学最推荐、最方便的 f-string,再了解其他方法。

1. 用 + 拼接(最基础)

name = "小明"age = 10print("我叫" + name + ",今年" + str(age) + "岁")

缺点:变量多了很麻烦,而且数字要转成字符串。

2. f-string(最方便、最流行,推荐使用!)

在字符串前面加个 f,然后直接在占位符 {} 里写变量,超级方便!f-string 是 Python 3.6 以后才有的,但现在几乎所有新代码都用它。

print(f"我叫{name},今年{age}岁")

你甚至可以在 {} 里写简单的表达式:

a, b = 53print(f"{a} + {b} = {a + b}")   # 输出 "5 + 3 = 8"

格式说明符(放在花括号里,冒号后):

# 对齐与宽度print(f"{'aaabbbccc':<10}")   # 左对齐,宽度10print(f"{'aaabbbccc':>10}")   # 右对齐print(f"{'aaabbbccc':^10}")   # 居中对齐print(f"{'aaabbbccc':*^10}")  # 填充字符*,居中# 数字格式化print(f"{3.14159:.2f}")      # 保留2位小数:3.14print(f"{1000000:,}")        # 千位分隔符:1,000,000print(f"{0.857:.2%}")        # 百分比:85.70%print(f"{10:b}")             # 二进制:1010print(f"{255:x}")            # 十六进制:ff

f-string 的冷门知识

  • 引号:f-string本身可以用双引号或单引号,内部可以用另一种引号。

    print(f'他说:"{name}"')
  • 转义大括号:如果想打印字面的大括号 {},要重复两次 {{ 和 }}

    print(f"{{name}}")   # 输出 {name}
  • 表达式可以很复杂:可以调用函数、进行运算。

    print(f"长度:{len('Python')}")print(f"大写:{'hello'.upper()}")
  • 多行f-string:可以用三引号。

    msg = f"""姓名:{name}年龄:{age}"""print(msg)

3. % 格式化(老方法,但有些地方还在用)

老方法就不用占位符 {} 了,我们使用占位符 % :

  • %s 字符串

  • %d 整数

  • %f 浮点数

  • %x 十六进制整数

  • %% 打印一个百分号

print("我叫%s,今年%d岁" % (name, age))

字符串后还要再跟一个 % ,在 % 后面的变量会依次填入占位符中,多个变量要用括号括起来(单个变量可以不用)。

格式说明符:可以控制宽度、小数位数等。

print("成绩:%5.2f" % 95.678)  # 输出 "成绩:95.68"(总宽5,小数2位)print("姓名:%10s" % "小明")   # 输出 "姓名:       小明"(右对齐,宽度10print("姓名:%-10s" % "小明")  # 输出 "姓名:小明       "(左对齐)

4. format 方法

也是用占位符 {} ,但是要在整个字符串后加上 .format() ,在 .format() 里面的变量会依次填入占位符中。

print("我叫{},今年{}岁".format(name, age))

可以指定位置:

print("我叫{0},{0}今年{1}岁".format(name, age))

可以指定关键字(默认值 小红 和 9 可以不写,此时必须得有变量值):

print("我叫{name},今年{age}岁".format(name="小红", age=9))

格式说明符(与 f-string 相同):

# 对齐与宽度print("{:<10}".format("左"))   # 左对齐,宽度10print("{:>10}".format("右"))   # 右对齐print("{:^10}".format("中"))   # 居中对齐print("{:*^10}".format("中"))  # 填充字符*,居中# 数字格式化print("{:.2f}".format(3.14159))      # 保留2位小数:3.14print("{:,}".format(1000000))        # 千位分隔符:1,000,000print("{:.2%}".format(0.857))        # 百分比:85.70%print("{:b}".format(10))             # 二进制:1010print("{:x}".format(255))            # 十六进制:ff

推荐:以后写代码,优先使用 f-string,它最简洁、最直观。只有在需要兼容老版本Python时才考虑其他方式。

试试看

  1. 用f-string输出:"小明,你的成绩是95.5分"(变量:name, score)。

  2. 用f-string输出一个数字,保留3位小数,宽度10,右对齐。

  3. %格式化输出一个百分比(比如0.857 → 85.7%)。


十一、字符串的常用函数和方法

Python给字符串准备了很多“工具”,可以帮我们做各种事情。下面我们一一介绍,每个都配有例子和练习。

1. 获取长度 len()

s = "Python"print(len(s))   # 输出 6

2. 大小写转换

s = "Hello, World!"print(s.upper())   # 全部大写:HELLO, WORLD!print(s.lower())   # 全部小写:hello, world!print(s.capitalize())  # 首字母大写其他小写:Hello, world!print(s.title())       # 每个单词首字母大写:Hello, World!print(s.swapcase())    # 大小写互换:hELLO, wORLD!

试试看:输入一个单词,输出它的大小写互换版本。

3. 去空格(strip)

s = "   Hello   "print(s.strip())        # 去掉两端的空格:Helloprint(s.lstrip())       # 去掉左边的空格:Hello   print(s.rstrip())       # 去掉右边的空格:   Hello

也可以去掉指定的字符:

s = "***Hello***"print(s.strip("*"))     # 去掉两边的*:Hello

试试看:输入一个带有前后空格的字符串,去掉空格后输出。

4. 查找子串

  • find():返回子串第一次出现的索引,如果没找到返回 -1。

  • index():和find类似,但没找到会报错。

  • rfind():从右边开始查找,返回最后一次出现的索引。

s = "Hello, World!"print(s.find("World"))   # 输出 7print(s.find("Python"))  # 输出 -1print(s.index("World"))  # 输出 7# print(s.index("Python"))  # 报错print(s.rfind("o"))      # 输出 8(最后一个o的位置)

还可以指定查找的范围:

print(s.find("o"5))    # 从索引5开始找 "o",输出 8print(s.find("o"59))    # 在索引5-9里(不含9)找 "o",输出 8

试试看:输入一段英文,找出第一次出现字母e的位置。

5. 替换子串 replace()

s = "I like apples"new_s = s.replace("apples""bananas")  # 把字符串中所有的 "apples" 都替换成 "bananas"print(new_s)  # 输出 "I like bananas"

可以指定替换次数:

s = "one one one"print(s.replace("one""two"2))  # 只替换前2个:two two one

6. 分割和连接

  • split():将字符串按指定分隔符拆分成列表。

  • join():将列表里的字符串用指定分隔符连接起来。

# 分割s = "苹果,香蕉,橙子"fruits = s.split(",")print(fruits)  # 输出 ['苹果', '香蕉', '橙子']# 连接new_s = "-".join(fruits)print(new_s)   # 输出 "苹果-香蕉-橙子"

split() 如果不给参数,默认按空白字符(空格、换行、制表符等)分割。

s = "Hello   World"print(s.split())  # 输出 ['Hello', 'World']

试试看:输入一个句子,按空格分割成单词列表,然后用逗号重新连接。

7. 判断开头/结尾

filename = "report.pdf"print(filename.startswith("report"))  # Trueprint(filename.endswith(".pdf"))      # True

可以传入元组,同时匹配多个:

print(filename.endswith((".pdf"".txt")))  # True

8. 判断字符串内容

这些方法用来判断字符串的组成:

方法
作用
isdigit()
是否全是数字
isalpha()
是否全是字母
isalnum()
是否全是字母或数字
isspace()
是否全是空白字符
islower()
是否全是小写
isupper()
是否全是大写
istitle()
是否每个单词首字母大写
print("123".isdigit())   # Trueprint("abc".isalpha())   # Trueprint("abc123".isalnum())# Trueprint("   ".isspace())   # True

9. 计数 count()

统计子串出现的次数。

s = "abracadabra"print(s.count("a"))   # 输出 5

还可以指定统计的范围:

print(s.count("a"5))    # 从索引5开始找 "a",输出 3print(s.count("a"59))    # 在索引5-9里(不含9)找 "a",输出 2

10. 对齐方法

  • center(width, fillchar):居中

  • ljust(width, fillchar):左对齐

  • rjust(width, fillchar):右对齐

s = "Python"print(s.center(10"*"))   # "**Python**"print(s.ljust(10"-"))    # "Python----"print(s.rjust(10"-"))    # "----Python"

十二、转义字符——让特殊符号“现原形”

请小朋友们尝试使用print()输出这句话:peppa:" i'm 13 years old "

会发现不使用三引号的情况下,其实没有办法输出对吧,这种情况就需要转义字符来帮助我们。

转义字符,就是转变一个字符原来的意思。转义字符以反斜杠 \ 开头,后面跟一个或者几个字符。反斜杠 \ 就代表一个转义功能,转变了后面字符原本的作用。

转义字符
含义
示例
\n
换行
print("第一行\n第二行")
\t
制表符(Tab)
print("姓名\t年龄")
\\
反斜杠本身
print("C:\\Python")
\'
单引号
print('It\'s a cat')
\"
双引号
print("他说:\"你好\"")
\r
回车(将光标移到行首)
print("Hello\rWorld")
\b
退格
print("Hello\bWorld")
\xhh
十六进制字符
print("\x48\x69")
 输出 "Hi"

例子:打印一首诗

print("床前明月光,\n疑是地上霜。\n举头望明月,\n低头思故乡。")

如果想打印一个反斜杠,就要写两个:

print("C:\\Users\\小明")  # 输出 C:\Users\小明

原始字符串:如果你不想让转义字符生效,可以在字符串前面加 r(raw string)。

print(r"C:\Users\小明")   # 输出 C:\Users\小明,不需要写两个反斜杠

试试看:用转义字符打印一个简单的表格(用\t对齐)。然后用原始字符串打印一个包含多个反斜杠的路径。


十三、冷门知识点——高手才知道的秘密(选读)

1. 字符串驻留(String Interning)

Python会自动重用某些短字符串,以节省内存。这叫做“字符串驻留”。通常只对看起来像标识符的字符串(字母、数字、下划线)生效。

a = "hello"b = "hello"print(a is b)   # 通常输出 Truec = "hello world"d = "hello world"print(c is d)   # 可能输出 False(因为包含空格,不一定驻留)

2. 多行字符串的缩进问题

当你在代码里写多行字符串时,缩进会被包含进去。可以用 textwrap.dedent 来去掉公共缩进。

import textwraps = textwrap.dedent("""    第一行    第二行""")print(s)

3. 字符串乘法的性能

字符串乘法 "*" 对于大量重复非常高效,因为它预先分配内存。

# 快速生成一个很长的字符串long_str = "=" * 1000000

4. split() 和 rsplit()

rsplit() 从右边开始分割,可以限制分割次数。

s = "a,b,c,d"print(s.rsplit(","2))   # 输出 ['a,b''c''d']

5. partition() 和 rpartition()

将字符串分成三部分:分隔符前、分隔符、分隔符后。

s = "Hello, World!"print(s.partition(", "))  # 输出 ('Hello'', ''World!')

6. maketrans() 和 translate()

用于批量替换字符。

# 把 'abc' 分别替换成 '123'trans = str.maketrans("abc""123")s = "a cat and a dog"print(s.translate(trans))  # 输出 "1 1t3t 1nd 1 dog"

7. format_map()

类似于 format(),但接受字典。

data = {"name""小明""age"10}print("{name}今年{age}岁".format_map(data))

8. 字符串的 __add__ 和 __mul__ 魔法方法

字符串支持 + 和 * 是因为内部定义了 __add__ 和 __mul__ 方法。你可以通过 dir(str) 查看所有方法。

9. 用 in 检查子串时的时间复杂度

对于字符串,in 的平均时间复杂度是 O(n),即需要扫描整个字符串。但对于很短的子串,速度很快。

10. 字符串编码和解码

字符串在计算机里是以字节形式存储的。你可以用 encode() 转成字节,用 decode() 转回字符串。

s = "中文"b = s.encode("utf-8")   # b'\xe4\xb8\xad\xe6\x96\x87'print(b.decode("utf-8")) # 输出 "中文"

十四、综合应用:让字符串魔法大显身手

让我们写几个小游戏,综合运用学到的知识。

挑战1:密码强度检测器

import stringpassword = input("请输入密码:")has_digit = any(ch.isdigit() for ch in password)has_alpha = any(ch.isalpha() for ch in password)has_special = any(ch in string.punctuation for ch in password)if len(password) < 8:    print("太短了,密码至少8个字符")elif has_digit and has_alpha and has_special and len(password) >= 12:    print("强密码!")elif has_digit and has_alpha and len(password) >= 8:    print("中等强度")elif has_digit and not has_alpha:    print("纯数字,太弱")elif has_alpha and not has_digit:    print("纯字母,太弱")else:    print("弱密码")

挑战2:单词反转器

sentence = input("请输入一个句子:")words = sentence.split()reversed_words = [word[::-1] for word in words]new_sentence = " ".join(reversed_words)print(new_sentence)

挑战3:回文检测器

s = input("请输入一个字符串:")clean = s.replace(" """).lower()if clean == clean[::-1]:    print("是回文")else:    print("不是回文")

挑战4:凯撒密码加密

def caesar(text, shift):    result = ""    for ch in text:        if ch.isupper():            result += chr((ord(ch) - ord('A') + shift) % 26 + ord('A'))        elif ch.islower():            result += chr((ord(ch) - ord('a') + shift) % 26 + ord('a'))        else:            result += ch    return resulttext = input("请输入要加密的文字:")shift = int(input("请输入偏移量(整数):"))print("加密后:", caesar(text, shift))

挑战5:提取文件名后缀(切片练习)

filename = input("请输入文件名:")dot_index = filename.find(".")if dot_index != -1:    suffix = filename[dot_index+1:]    print(f"文件后缀是:{suffix}")else:    print("没有后缀")

十五、总结

今天我们学习了字符串的各种魔法:

  • 定义:用引号包起来。

  • 拼接和重复+ 和 *

  • 索引:正索引(0开始)和负索引(-1开始)。

  • 切片[起始:结束:步长],可以省略起始、结束、步长,实现取子串、跳着取、反转等。

  • 不可变性和id:字符串不能修改,id() 查看内存地址,理解字符串驻留。

  • 比较大小:按字符编码顺序比较。

  • 成员检查in 和 not in

  • 格式化+ 拼接、f-string(推荐)、% 格式化、format 方法,包括对齐、宽度、小数位、千位分隔符等。

  • 常用方法len、大小写转换、去空格、查找、替换、分割、连接、判断开头结尾、判断内容等。

  • 转义字符\n\t\\ 等,以及原始字符串 r""

  • 冷门知识点:字符串驻留、多行字符串缩进、partitiontranslatemaketrans、编码解码等。

现在,你已经掌握了字符串的魔法,快去用它来写故事、做游戏吧!

小作业

  1. 用切片取出字符串 "Python" 的 "yth" 部分。

  2. 反转字符串 "123456789",看看结果。

  3. 输入一句话,把里面的空格都替换成 "_",然后输出。

  4. 输入一个单词,判断它是否以元音字母(a,e,i,o,u)开头(不区分大小写)。

  5. 挑战:用 translate 方法实现一个简单的密码替换:将 "abc" 替换成 "123"

遇到问题可以问问爸爸妈妈,或者在评论区留言。我们下次魔法课堂见!

最新文章

随机文章

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-04-20 01:35:08 HTTP/2.0 GET : https://f.mffb.com.cn/a/485013.html
  2. 运行时间 : 0.116697s [ 吞吐率:8.57req/s ] 内存消耗:4,658.14kb 文件加载:140
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=5389768184aec7bec6a9e116cfcfd69b
  1. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/public/index.php ( 0.79 KB )
  2. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/autoload.php ( 0.17 KB )
  3. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_real.php ( 2.49 KB )
  4. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/platform_check.php ( 0.90 KB )
  5. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/ClassLoader.php ( 14.03 KB )
  6. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_static.php ( 4.90 KB )
  7. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper.php ( 8.34 KB )
  8. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/helper.php ( 2.19 KB )
  9. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/helper.php ( 1.47 KB )
  10. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/stubs/load_stubs.php ( 0.16 KB )
  11. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Exception.php ( 1.69 KB )
  12. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Facade.php ( 2.71 KB )
  13. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/deprecation-contracts/function.php ( 0.99 KB )
  14. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap.php ( 8.26 KB )
  15. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap80.php ( 9.78 KB )
  16. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/Resources/functions/dump.php ( 1.49 KB )
  17. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-dumper/src/helper.php ( 0.18 KB )
  18. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/VarDumper.php ( 4.30 KB )
  19. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/App.php ( 15.30 KB )
  20. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Container.php ( 15.76 KB )
  21. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/container/src/ContainerInterface.php ( 1.02 KB )
  22. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/provider.php ( 0.19 KB )
  23. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Http.php ( 6.04 KB )
  24. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Str.php ( 7.29 KB )
  25. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Env.php ( 4.68 KB )
  26. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/common.php ( 0.03 KB )
  27. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/helper.php ( 18.78 KB )
  28. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Config.php ( 5.54 KB )
  29. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/app.php ( 0.95 KB )
  30. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cache.php ( 0.78 KB )
  31. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/console.php ( 0.23 KB )
  32. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cookie.php ( 0.56 KB )
  33. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/database.php ( 2.48 KB )
  34. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Env.php ( 1.67 KB )
  35. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/filesystem.php ( 0.61 KB )
  36. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/lang.php ( 0.91 KB )
  37. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/log.php ( 1.35 KB )
  38. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/middleware.php ( 0.19 KB )
  39. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/route.php ( 1.89 KB )
  40. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/session.php ( 0.57 KB )
  41. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/trace.php ( 0.34 KB )
  42. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/view.php ( 0.82 KB )
  43. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/event.php ( 0.25 KB )
  44. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Event.php ( 7.67 KB )
  45. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/service.php ( 0.13 KB )
  46. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/AppService.php ( 0.26 KB )
  47. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Service.php ( 1.64 KB )
  48. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Lang.php ( 7.35 KB )
  49. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/lang/zh-cn.php ( 13.70 KB )
  50. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/Error.php ( 3.31 KB )
  51. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/RegisterService.php ( 1.33 KB )
  52. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/services.php ( 0.14 KB )
  53. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/PaginatorService.php ( 1.52 KB )
  54. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ValidateService.php ( 0.99 KB )
  55. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ModelService.php ( 2.04 KB )
  56. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Service.php ( 0.77 KB )
  57. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Middleware.php ( 6.72 KB )
  58. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/BootService.php ( 0.77 KB )
  59. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Paginator.php ( 11.86 KB )
  60. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/Validate.php ( 63.20 KB )
  61. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Model.php ( 23.55 KB )
  62. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Attribute.php ( 21.05 KB )
  63. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/AutoWriteData.php ( 4.21 KB )
  64. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Conversion.php ( 6.44 KB )
  65. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/DbConnect.php ( 5.16 KB )
  66. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/ModelEvent.php ( 2.33 KB )
  67. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/RelationShip.php ( 28.29 KB )
  68. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Arrayable.php ( 0.09 KB )
  69. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Jsonable.php ( 0.13 KB )
  70. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/contract/Modelable.php ( 0.09 KB )
  71. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Db.php ( 2.88 KB )
  72. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/DbManager.php ( 8.52 KB )
  73. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Log.php ( 6.28 KB )
  74. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Manager.php ( 3.92 KB )
  75. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerTrait.php ( 2.69 KB )
  76. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerInterface.php ( 2.71 KB )
  77. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cache.php ( 4.92 KB )
  78. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/simple-cache/src/CacheInterface.php ( 4.71 KB )
  79. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Arr.php ( 16.63 KB )
  80. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/driver/File.php ( 7.84 KB )
  81. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/Driver.php ( 9.03 KB )
  82. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/CacheHandlerInterface.php ( 1.99 KB )
  83. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/Request.php ( 0.09 KB )
  84. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Request.php ( 55.78 KB )
  85. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/middleware.php ( 0.25 KB )
  86. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Pipeline.php ( 2.61 KB )
  87. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/TraceDebug.php ( 3.40 KB )
  88. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/middleware/SessionInit.php ( 1.94 KB )
  89. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Session.php ( 1.80 KB )
  90. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/driver/File.php ( 6.27 KB )
  91. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/SessionHandlerInterface.php ( 0.87 KB )
  92. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/Store.php ( 7.12 KB )
  93. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Route.php ( 23.73 KB )
  94. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleName.php ( 5.75 KB )
  95. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Domain.php ( 2.53 KB )
  96. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleGroup.php ( 22.43 KB )
  97. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Rule.php ( 26.95 KB )
  98. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleItem.php ( 9.78 KB )
  99. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/route/app.php ( 1.72 KB )
  100. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Route.php ( 4.70 KB )
  101. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/dispatch/Controller.php ( 4.74 KB )
  102. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Dispatch.php ( 10.44 KB )
  103. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/controller/Index.php ( 4.81 KB )
  104. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/BaseController.php ( 2.05 KB )
  105. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/facade/Db.php ( 0.93 KB )
  106. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/connector/Mysql.php ( 5.44 KB )
  107. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/PDOConnection.php ( 52.47 KB )
  108. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Connection.php ( 8.39 KB )
  109. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/ConnectionInterface.php ( 4.57 KB )
  110. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/builder/Mysql.php ( 16.58 KB )
  111. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Builder.php ( 24.06 KB )
  112. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseBuilder.php ( 27.50 KB )
  113. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Query.php ( 15.71 KB )
  114. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseQuery.php ( 45.13 KB )
  115. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TimeFieldQuery.php ( 7.43 KB )
  116. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/AggregateQuery.php ( 3.26 KB )
  117. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php ( 20.07 KB )
  118. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ParamsBind.php ( 3.66 KB )
  119. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ResultOperation.php ( 7.01 KB )
  120. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/WhereQuery.php ( 19.37 KB )
  121. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/JoinAndViewQuery.php ( 7.11 KB )
  122. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php ( 2.63 KB )
  123. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/Transaction.php ( 2.77 KB )
  124. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/driver/File.php ( 5.96 KB )
  125. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/LogHandlerInterface.php ( 0.86 KB )
  126. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/Channel.php ( 3.89 KB )
  127. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/event/LogRecord.php ( 1.02 KB )
  128. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/Collection.php ( 16.47 KB )
  129. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/View.php ( 1.70 KB )
  130. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/View.php ( 4.39 KB )
  131. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Response.php ( 8.81 KB )
  132. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/response/View.php ( 3.29 KB )
  133. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cookie.php ( 6.06 KB )
  134. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-view/src/Think.php ( 8.38 KB )
  135. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/TemplateHandlerInterface.php ( 1.60 KB )
  136. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/Template.php ( 46.61 KB )
  137. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/driver/File.php ( 2.41 KB )
  138. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/contract/DriverInterface.php ( 0.86 KB )
  139. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/runtime/temp/067d451b9a0c665040f3f1bdd3293d68.php ( 11.98 KB )
  140. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Html.php ( 4.42 KB )
  1. CONNECT:[ UseTime:0.000448s ] mysql:host=127.0.0.1;port=3306;dbname=f_mffb;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.000628s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.001188s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.000501s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.000617s ]
  6. SELECT * FROM `set` [ RunTime:0.000211s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.000589s ]
  8. SELECT * FROM `article` WHERE `id` = 485013 LIMIT 1 [ RunTime:0.004083s ]
  9. UPDATE `article` SET `lasttime` = 1776620108 WHERE `id` = 485013 [ RunTime:0.001103s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 66 LIMIT 1 [ RunTime:0.001074s ]
  11. SELECT * FROM `article` WHERE `id` < 485013 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.000533s ]
  12. SELECT * FROM `article` WHERE `id` > 485013 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.003490s ]
  13. SELECT * FROM `article` WHERE `id` < 485013 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.002801s ]
  14. SELECT * FROM `article` WHERE `id` < 485013 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.004068s ]
  15. SELECT * FROM `article` WHERE `id` < 485013 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.002305s ]
0.118237s