在 C# 里获取字符串的子串,你得写 s.Substring(1, 3)——参数是起始位置和长度。
在 Python 里?直接写 s[1:4]——就像切蛋糕一样,切出第 1 到第 4 个字符。
当我第一次用 Python 切片的时候,我心想:"这语法也太优雅了吧?"
还有那个字符串反转——C# 得用 new string(s.Reverse().ToArray()),Python 一行搞定:s[::-1]。
今天咱们来聊聊字符串操作——Python 的切片语法是真正的"语法级"支持。
C# 版本:
string s1 = "Hello";string s2 = "World";string s3 = @"C:\path\to\file"; // 原始字符串string s4 = $@"Hello {name}"; // 插值原始字符串string result = s1 + " " + s2;string interpolated = $"{s1}{s2}";string formatted = $"Price: {price:F2}";string padded = s1.PadRight(10, '.'); // "Hello....."Python 版本:
s1 = "Hello"s2 = "World"s3 = r"C:\path\to\file"# 原始字符串s4 = f"Hello {name}"# f-string(推荐)result = s1 + " " + s2interpolated = f"{s1}{s2}"formatted = f"Price: {price:.2f}"padded = s1.ljust(10, '.') # "Hello....."string | str | |
@"..." | r"..." | |
$"...{var}..." | f"...{var}..." | |
$"""...""" | '''...'''"""...""" | |
$"{val:F2}" | f"{val:.2f}" | |
s.PadRight(10, '.') | s.ljust(10, '.') |
C# 用 $"..." 做字符串插值,Python 用 f"..."——语法几乎一样,只是前缀不同。
为什么 Python 用 f-string 而不是 $string?
因为 f 是 "format" 的缩写,更直观。而且 f-string 是 Python 3.6 引入的,比 C# 的字符串插值(C# 6)晚了两年,但语法更简洁。
Python 的 f-string 格式化语法比 C# 的插值字符串更紧凑,但两者功能覆盖面相似:
price = 3.14159name = "Python"padding = 42# ── 基础格式化 ──f"{price:.2f}"# "3.14" 保留2位小数f"{price:10.2f}"# " 3.14" 宽度10,右对齐(默认)f"{price:<10.2f}"# "3.14 " 左对齐f"{price:^10.2f}"# " 3.14 " 居中对齐f"{price:*>10.2f}"# "******3.14" 用 * 填充# ── 整数格式化 ──f"{padding:08d}"# "00000042" 前导零填充f"{padding:,d}"# "42" 千分位分隔(大数有用)f"{255:#x}"# "0xff" 十六进制带前缀f"{255:#06X}"# "0X00FF" 大写十六进制,宽度6# ── 百分比与符号 ──f"{0.5:.1%}"# "50.0%" 百分比保留1位f"{-42:+d}"# "-42" 强制显示符号# ── 调试语法(Python 3.8+,C# .NET 6+ 也有) ──x = 42f"{x = }"# "x = 42" 自动打印变量名和值f"{x + 1 = }"# "x + 1 = 43" 支持表达式C# 对比(插值字符串的对齐和格式化):
double price = 3.14159;// C# 的对齐和格式化$"{price,10:F2}"// " 3.14" 右对齐,宽度10$"{price,-10:F2}"// "3.14 " 左对齐$"{price:*^10:F2}"// "***3.14***" 居中,*填充// C# 也有千分位和百分比格式$"{1234567:N0}"// "1,234,567" N 格式:千分位分隔$"{0.5:P1}"// "50.0 %" P 格式:百分比(注意有空格)// 调试语法 $"{x = }" 要到 .NET 6 才有:<:^:> | ,- | |
f"{x:*^10}" | $"{x,*^10}" | |
f"{x:,}" | $"{x:N0}" | |
f"{x:#x}" | $"{x:X}" | |
f"{x:.1%}" | $"{x:P1}" | |
f"{x = }" | $"{x = }" | |
迁移口诀:Python f-string 和 C# 插值字符串功能覆盖面相似,但 f-string 语法更紧凑(对齐、填充、调试更简洁)。C# 开发者迁移到 Python 后会发现格式化写法更短更清晰。
这是 Python 最优雅的地方。但 C# 8.0+ 也跟上了——引入了索引和范围。
C# 版本:
string s = "Hello, World!";// 单个字符访问char first = s[0]; // 'H'char last = s[s.Length - 1]; // '!'char last2 = s[^1]; // '!'(C# 8.0+ 负数索引)// 子串string sub1 = s.Substring(0, 5); // "Hello"string sub2 = s.Substring(7); // "World!"// C# 8.0+ 范围语法string sub3 = s[0..5]; // "Hello"(等同于 Substring(0, 5))string sub4 = s[7..]; // "World!"(从索引7到末尾)string sub5 = s[..5]; // "Hello"(从开头到索引5)string sub6 = s[^6..]; // "World!"(从倒数第6个到末尾)// 查找int index = s.IndexOf("World"); // 7bool contains = s.Contains("Hello"); // truebool starts = s.StartsWith("Hello"); // truebool ends = s.EndsWith("World!"); // true// 替换string replaced = s.Replace("World", "Python"); // "Hello, Python!"// 分割string[] parts = s.Split(','); // ["Hello", " World!"]// 合并string joined = string.Join("-", new[] {"a", "b", "c"}); // "a-b-c"Python 版本:
s = "Hello, World!"# 单个字符访问first = s[0] # 'H'last = s[-1] # '!'(负数索引!)# 切片(Python 的杀手锏)sub1 = s[0:5] # "Hello"sub2 = s[7:] # "World!"sub3 = s[:5] # "Hello"sub4 = s[::2] # "Hlo ol!"(每隔一个字符)sub5 = s[::-1] # "!dlroW ,olleH"(反转!)# 查找index = s.find("World") # 7contains = "Hello"in s # Truestarts = s.startswith("Hello") # Trueends = s.endswith("World!") # True# 替换replaced = s.replace("World", "Python") # "Hello, Python!"# 分割parts = s.split(',') # ["Hello", " World!"]# 合并joined = "-".join(["a", "b", "c"]) # "a-b-c"s[^1] | s[-1] | |
s[0..5] | s[0:5] | |
Reverse() | s[::-1] | |
IndexOf() | find() | |
Contains() | in |
Python 的 s[-1] 直接访问最后一个字符,C# 8.0+ 也支持了 s[^1]。Python 的 s[::-1] 直接反转字符串,C# 得用 LINQ 的 Reverse()。差距在缩小,但 Python 仍然更简洁。
为什么 Python 的切片这么强大?
因为 Python 的切片是语法级支持,而 C# 的 Substring() 只是一个方法调用。切片语法可以处理任意步长、负数索引、省略参数,非常灵活。
真实场景:在处理文件路径时,切片非常方便:
# 获取文件扩展名path = "/home/user/document.txt"ext = path[path.rfind('.') + 1:] # "txt"# 获取文件名(不含扩展名)name = path[path.rfind('/') + 1:path.rfind('.')] # "document"C# 得这样写:
string path = "/home/user/document.txt";string ext = Path.GetExtension(path); // ".txt"string name = Path.GetFileNameWithoutExtension(path); // "document"Python 的切片更通用,C# 需要专门的 Path 类。
s.Length | len(s) | |
s.ToUpper() | s.upper() | |
s.ToLower() | s.lower() | |
s.Trim() | s.strip() | |
s.TrimStart() | s.lstrip() | |
s.TrimEnd() | s.rstrip() | |
s.PadLeft(10, ' ') | s.rjust(10) | |
s.PadRight(10, ' ') | s.ljust(10) | |
s.PadLeft((s.Length+10)/2).PadRight(10) | s.center(10) | |
s.Replace("a", "b") | s.replace("a", "b") | |
s.Split(',') | s.split(',') | |
string.Join(",", arr) | ",".join(arr) | |
int.TryParse(s, out _) | s.isdigit() | |
char.IsLetter(c) | c.isalpha() |
C# 的方法名是大写开头(ToUpper、Replace),Python 的是小写(upper、replace)。C# 用属性(s.Length),Python 用函数(len(s))。
为什么 Python 的方法名是小写?
因为 Python 的命名规范(PEP 8)规定:函数和方法用小写下划线,类用大写驼峰。这是 Python 的"社区约定"。
C# 和 Python 的字符串都是不可变的:
// C# 字符串不可变string s = "Hello";// s[0] = 'h'; // 编译错误s = "hello"; // 创建新字符串,重新赋值// StringBuilder 用于频繁修改var sb = new StringBuilder("Hello");sb[0] = 'h';sb.Append(" World");string result = sb.ToString();# Python 字符串也不可变s = "Hello"# s[0] = 'h' # TypeErrors = "hello"# 创建新字符串,重新赋值# 如果需要频繁修改,用列表chars = list(s) # ['H', 'e', 'l', 'l', 'o']chars[0] = 'h'# 可以修改s = ''.join(chars) # 转回字符串C# 有 StringBuilder 用于频繁修改,Python 没有等价物——想频繁修改就用列表,最后再 join 回字符串。
真实场景:在构建 SQL 查询或 HTML 时,经常需要拼接字符串:
# Python 方式parts = []for i in range(1000): parts.append(f"item_{i}")result = ", ".join(parts)// C# 方式var sb = new StringBuilder();for (int i = 0; i < 1000; i++){if (i > 0) sb.Append(", "); sb.Append($"item_{i}");}string result = sb.ToString();Python 的 join() 更简洁,C# 的 StringBuilder 更高效。
C# 的字符串操作是"方法驱动"的——s.Substring()、s.Replace()、s.Split(),每个操作都是方法调用。
Python 的字符串操作是"语法驱动"的——s[1:4] 比 Substring 更直观,"x" in s 比 Contains() 更简洁,",".join(list) 比 string.Join() 更优雅。
C# 的字符串操作像是在用工具箱,每个工具都有明确的用途; Python 的字符串操作像是在用瑞士军刀,一个工具搞定大部分场景。
s[-1] 是最后一个字符s[1:4] 是从索引 1 到 4(不包括 4)len() 是属性:Python 用 len(s) 函数+ 拼接:Python 用 join() 更高效切片不越界——Python 切片不会报错:
s = "Hello"print(s[0:100]) # "Hello"(不会报错)print(s[100:200]) # ""(空字符串)f-string 的表达式——f-string 可以包含表达式:
name = "World"print(f"Hello, {name.upper()}!") # "Hello, WORLD!"print(f"{2 + 3 = }") # "2 + 3 = 5"(Python 3.8+)多行字符串——开头的引号后面不能有内容:
# 这样会报错# s = """ 这会报错 """ # SyntaxError# 正确写法s = """这是正确的多行字符串"""Python 的切片语法
s[1:4]是真正的"语法级"支持,C# 得靠Substring方法调用。
下一篇咱们来聊聊条件判断——Python 的 elif 比 C# 的 else if 少打一个空格,这就是 Python 的"懒人哲学"。