# 检查字符串是否只包含字母 s = "HelloWorld" print(all(c in string.ascii_letters for c in s)) # 输出 True
# 删除字符串中的标点符号 s = "Hello, World!" print("".join(c for c in s if c notin string.punctuation)) # 输出 HelloWorld
# 格式化字符串 name = "Alice" age = 30 formatted_string = string.Template('$name is $age years old.') print(formatted_string.substitute(name=name, age=age)) # 输出 Alice is 30 years old.
# 使用模板字符串 from string import Template
name = "Alice" age = 30 t = Template('$name is $age years old.') print(t.substitute(name=name, age=age)) # 输出 Alice is 30 years old.
# 字符串模板替换 s = "$who likes $what" d = {'who': 'Alice', 'what': 'Python'} print(string.Template(s).safe_substitute(d)) # 输出 Alice likes Python