当前位置:首页>python>【C#vsPython·第一阶段】 字符串处理:Python 让你少写一半代码

【C#vsPython·第一阶段】 字符串处理:Python 让你少写一半代码

  • 2026-10-11 07:48:18
【C#vsPython·第一阶段】 字符串处理:Python 让你少写一半代码

在 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....."
对比项
C#
Python
字符串类型
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)晚了两年,但语法更简洁。

f-string 格式化详解(Python 更简洁的写法)

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 才有
特性
Python f-string
C# 插值字符串
对齐
:<:^:>,-
 前缀
填充
f"{x:*^10}"$"{x,*^10}"
千分位
f"{x:,}"$"{x:N0}"
十六进制
f"{x:#x}"$"{x:X}"
百分比
f"{x:.1%}"$"{x:P1}"
(注意输出带空格)
调试
f"{x = }"
 (3.8+)
$"{x = }"
 (.NET 6+)
嵌套表达式
✅ 任意表达式
✅ 任意表达式

迁移口诀: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"
特性
C#
Python
负数索引
s[^1]
(C# 8.0+)
s[-1]
切片语法
s[0..5]
(C# 8.0+)
s[0:5]
反转
Reverse()
 + LINQ
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 类。

字符串方法对比

功能
C#
Python
长度
s.Lengthlen(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 的字符串操作像是在用瑞士军刀,一个工具搞定大部分场景。

迁移指南:C# 开发者最容易犯的错

  1. 忘记负数索引:s[-1] 是最后一个字符
  2. 混淆切片参数:s[1:4] 是从索引 1 到 4(不包括 4)
  3. 以为 len() 是属性:Python 用 len(s) 函数
  4. 忘记字符串不可变:不能直接修改字符
  5. 过度使用 + 拼接: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 的"懒人哲学"。

最新文章

随机文章