码上问学问代码之理,学编程之道

Python 演练场

左边改代码,右边看输出 —— 不用装环境,浏览器里就能跑 Python。 改一改、点「运行」(或按 Ctrl+↵),立刻看到效果。各章里标着「可运行」的示例也都支持就地编辑运行。

string.py python 可运行
s = "码上问学"
print(len(s))                 # 长度:4(一个中文字算一个字符)
print(s[0])                   # 取第 1 个字符:知
print(s[0:2])                 # 切片:取前 2 个 → 知码

# 常用“整理文字”的方法
print("  hello  ".strip())    # 去掉两端空格 → hello
print("a-b-c".split("-"))     # 按 - 切开 → ['a', 'b', 'c']
print("-".join(["x", "y"]))   # 用 - 连接 → x-y
print("Python".lower())        # 转小写 → python
运行结果: 4 知 知码 hello ['a', 'b', 'c'] x-y python

提示:浏览器内的 Python 由 Brython 提供,支持标准库(print / 列表 / 字典 / 函数 / 文件字符串 IO 等)。 用到 pandas / requests / matplotlib 的进阶示例无法在浏览器里运行,需在本地 Python 环境执行。