# ===== CodeLab: Python 字符串处理全攻略 =====
# 来源: https://aoerliang.dpdns.org/articles/python-strings
# 以下代码片段按文章出现顺序拼接, 共 6 段

# ----- 片段 1 (python) -----
s1 = '单引号'
s2 = "双引号里可以有 ' 单引号"
s3 = """多行
字符串"""
print(len(s3))   # 长度包含换行符

# 原始字符串:路径不用转义
path = r"C:\Users\ada\notes.txt"
print(path)

# ----- 片段 2 (python) -----
text = "  Hello, Python World!  "
print(text.strip())       # 去掉两端空白
print(text.lstrip(" H"))  # 去掉左侧空格和 H
print(text.lower())       # 转小写
print(text.upper())       # 转大写
print(text.title())       # 每个单词首字母大写

# ----- 片段 3 (python) -----
s = "hello.py"
print(s.startswith("hello"))  # True
print(s.endswith(".py"))      # True
print(s.find("."))            # 5 找不到返回 -1
print(s.count("l"))           # 2 出现次数
print(s.replace("hello", "hi"))  # hi.py

print("12345".isdigit())  # True
print("abc".isalpha())    # True
print("abc123".isalnum()) # True

# ----- 片段 4 (python) -----
data = "苹果,香蕉,橙子,葡萄"
fruits = data.split(",")
print(fruits)          # ['苹果', '香蕉', '橙子', '葡萄']

print("-".join(fruits))    # 苹果-香蕉-橙子-葡萄
print(" ".join(["a", "b"]))  # a b

# 只切前两刀
parts = "1-2-3-4".split("-", 2)
print(parts)           # ['1', '2', '3-4']

# 按空白切分(任意数量空格都行)
print("a   b\tc".split())  # ['a', 'b', 'c']

# ----- 片段 5 (python) -----
name, score = "Ada", 97.5
print(f"{name} 的得分是 {score:.1f} 分")
print(f"正确率:{score/100:.2%}")     # 97.50%
print(f"{score:06.1f}")             # 097.5 补零

# 对齐与填充
print(f"|{name:<10}|")   # 左对齐
print(f"|{name:>10}|")   # 右对齐
print(f"|{name:^10}|")   # 居中

# 千分位与进制
print(f"{1234567:,}")     # 1,234,567
print(f"{255:#x}")        # 0xff

# ----- 片段 6 (python) -----
line = "  ERROR 2026-07-06 10:23:45  磁盘空间不足  "
line = line.strip().lower()
level, rest = line.split(" ", 1)
date, _, msg = rest.partition(" ")
print(f"级别:{level}")
print(f"时间:{date}")
print(f"信息:{msg}")

# 输出:
# 级别:error
# 时间:2026-07-06
# 信息:10:23:45  磁盘空间不足
