字符串(String)是由字符组成的有序序列,用于表示文本数据。在Python中,字符串是内置的序列类型之一,用单引号 ' '
或双引号 " "
括起来。
# 定义字符串的多种方式
s1 = 'hello' # 单引号
s2 = "world" # 双引号
s3 = '''多行
字符串''' # 三引号,支持多行
s4 = str(123) # 从其他类型转换
# 访问字符(索引从0开始)
print(s1[0]) # h
print(s2[-1]) # d (负索引从末尾开始)
print(s1[1:3]) # el (切片)
# 基本操作示例
s1 = 'hello'
s2 = 'world'
# 拼接与重复
s = s1 + ' ' + s2
print(s) # hello world
print(s1 * 3) # hellohellohello
# 长度与成员判断
print(len(s)) # 11
print('he' in s1) # True
print('xyz' not in s1) # True
# 比较操作
print('abc' < 'def') # True (按字典序)
print('hello' == 'hello') # True
string[start:end:step]
# 切片操作示例
text = 'abcdefghijklmnop'
# 基本切片
print(text[1:5]) # bcde
print(text[:5]) # abcde (从开头到索引5)
print(text[5:]) # fghijklmnop (从索引5到结尾)
print(text[:]) # abcdefghijklmnop (整个字符串)
# 负索引切片
print(text[-5:]) # lmnop (最后5个字符)
print(text[:-5]) # abcdefghijk (除了最后5个字符)
# 步长切片
print(text[::2]) # acegikmo (每隔一个字符)
print(text[1::2]) # bdfhjlnp (从索引1开始,每隔一个字符)
print(text[::-1]) # ponmlkjihgfedcba (反转字符串)
# 遍历字符串的多种方法
text = 'Python'
# 方法1:直接遍历字符
for ch in text:
print(ch, end=' ')
print() # 换行
# 方法2:通过索引遍历
for i in range(len(text)):
print(f"{i}: {text[i]}")
# 方法3:使用enumerate
for i, ch in enumerate(text):
print(f"位置{i}: 字符'{ch}'")
# 方法4:反向遍历
for ch in reversed(text):
print(ch, end=' ')
str.find(sub, start, end)
:返回子串首次出现的索引,未找到返回-1str.rfind(sub, start, end)
:从右侧开始查找str.index(sub, start, end)
:功能同find,但子串不存在时会报错str.rindex(sub, start, end)
:从右侧开始查找str.replace(old, new, count)
:替换子串,count指定替换次数str.count(sub, start, end)
:统计子串出现的次数
# 查找与替换示例
text = "Hello World Hello Python Hello"
# 查找操作
print(text.find("Hello")) # 0
print(text.rfind("Hello")) # 22
print(text.find("Python")) # 17
print(text.find("Java")) # -1 (未找到)
# 统计与替换
print(text.count("Hello")) # 3
print(text.replace("Hello", "Hi")) # Hi World Hi Python Hi
print(text.replace("Hello", "Hi", 2)) # 只替换前2次
str.lower()
:将所有字符转为小写str.upper()
:将所有字符转为大写str.title()
:将每个单词的首字母转为大写str.capitalize()
:仅将字符串首字母转为大写str.swapcase()
:大小写互换
# 大小写转换示例
s = "hello WORLD python"
print(s.lower()) # hello world python
print(s.upper()) # HELLO WORLD PYTHON
print(s.title()) # Hello World Python
print(s.capitalize()) # Hello world python
print(s.swapcase()) # HELLO world PYTHON
str.strip(chars)
:去除字符串两端的指定字符,默认去除空格str.lstrip(chars)
/ str.rstrip(chars)
:分别去除左侧/右侧str.center(width, fillchar)
:将字符串居中str.ljust(width, fillchar)
/ str.rjust(width, fillchar)
:左对齐/右对齐str.zfill(width)
:用0填充到指定宽度
# 空白处理与填充示例
s = " hello world "
num = "42"
print(s.strip()) # hello world
print(s.lstrip()) # hello world
print(s.rstrip()) # hello world
print(s.center(20, '*')) # ***hello world****
print(s.ljust(20, '-')) # hello world-------
print(s.rjust(20, '+')) # +++++++hello world
print(num.zfill(5)) # 00042
str.split(sep, maxsplit)
:按sep分割字符串为列表str.rsplit(sep, maxsplit)
:从右侧开始分割str.splitlines(keepends)
:按换行符分割str.join(iterable)
:将可迭代对象用当前字符串连接str.partition(sep)
:将字符串分为三部分:分隔符前、分隔符、分隔符后
# 分割与连接示例
text = "apple,banana,orange,grape"
multiline = "line1\nline2\nline3"
# 分割操作
fruits = text.split(',')
print(fruits) # ['apple', 'banana', 'orange', 'grape']
fruits2 = text.split(',', 2) # 最多分割2次
print(fruits2) # ['apple', 'banana', 'orange,grape']
lines = multiline.splitlines()
print(lines) # ['line1', 'line2', 'line3']
# 连接操作
result = '-'.join(fruits)
print(result) # apple-banana-orange-grape
# 分割为三部分
before, sep, after = text.partition(',')
print(f"前: {before}, 分隔符: {sep}, 后: {after}")
str.startswith(prefix)
:判断是否以prefix开头str.endswith(suffix)
:判断是否以suffix结尾str.isalpha()
:判断是否全为字母str.isdigit()
:判断是否全为数字str.isalnum()
:判断是否仅包含字母和数字str.isspace()
:判断是否全为空白字符str.islower()
/ str.isupper()
:判断是否全为小写/大写
# 字符串检查示例
s1 = "Hello123"
s2 = "12345"
s3 = " "
s4 = "hello"
s5 = "WORLD"
print(s1.isalnum()) # True
print(s2.isdigit()) # True
print(s3.isspace()) # True
print(s1.startswith("He")) # True
print(s1.endswith("23")) # True
print(s4.islower()) # True
print(s5.isupper()) # True
"Hello, %s" % name
(Python 2风格)"{} {}".format("Hello", "World")
(Python 3风格)f"I'm {age} years old"
(Python 3.6+,推荐)
# 字符串格式化示例
name = "Alice"
age = 18
city = "Beijing"
height = 1.65
# 1. % 格式化
print("Hello, %s" % name)
print("I'm %d years old" % age)
print("I live in %s and I'm %.2f meters tall" % (city, height))
# 2. format() 方法
print("Hello, {}".format(name))
print("I'm {} years old from {}".format(age, city))
print("Height: {:.2f}m".format(height))
# 3. f-字符串(推荐)
print(f"Hello, {name}")
print(f"I'm {age} years old from {city}")
print(f"Next year I'll be {age + 1} years old")
print(f"Height: {height:.2f}m")
print(f"Name: {name.upper()}, Age: {age}")
# 格式化规范示例
name = "Bob"
age = 25
salary = 50000.12345
# 数字格式化
print(f"Age: {age:05d}") # Age: 00025 (5位,不足补0)
print(f"Salary: ${salary:,.2f}") # Salary: $50,000.12 (千分位分隔符)
print(f"Percentage: {0.1234:.1%}") # Percentage: 12.3%
# 字符串对齐
print(f"Name: {name:>10}") # Name: Bob (右对齐)
print(f"Name: {name:<10}") # Name: Bob (左对齐)
print(f"Name: {name:^10}") # Name: Bob (居中)
# 条件表达式
status = "active" if age >= 18 else "inactive"
print(f"Status: {status}")
# 不可变性示例
s = "hello"
# 这些操作会报错:
# s[0] = 'H' # TypeError: 'str' object does not support item assignment
# 正确的"修改"方式:
s = 'H' + s[1:] # 创建新字符串
print(s) # Hello
# 或者使用字符串方法
s = s.replace('h', 'H') # 创建新字符串
print(s) # Hello
# 字符串与其他类型转换
# 其他类型转字符串
num = 123
lst = [1, 2, 3]
print(str(num)) # "123"
print(str(lst)) # "[1, 2, 3]"
# 字符串转其他类型
s1 = "123"
s2 = "3.14"
s3 = "hello"
print(int(s1)) # 123
print(float(s2)) # 3.14
print(list(s3)) # ['h', 'e', 'l', 'l', 'o']
# 安全转换(处理异常)
def safe_int(s):
try:
return int(s)
except ValueError:
return None
print(safe_int("123")) # 123
print(safe_int("abc")) # None
# 案例1:清理用户输入
def clean_input(user_input):
"""清理用户输入:去除首尾空格,转换为小写"""
return user_input.strip().lower()
# 案例2:检查密码强度
def check_password_strength(password):
"""检查密码强度"""
if len(password) < 8:
return "弱:密码长度不足8位"
has_upper = any(c.isupper() for c in password)
has_lower = any(c.islower() for c in password)
has_digit = any(c.isdigit() for c in password)
has_special = any(not c.isalnum() for c in password)
score = sum([has_upper, has_lower, has_digit, has_special])
if score == 4:
return "强:包含大小写字母、数字和特殊字符"
elif score >= 3:
return "中:包含3种字符类型"
else:
return "弱:字符类型单一"
# 测试
print(clean_input(" Hello World ")) # hello world
print(check_password_strength("abc123")) # 弱:字符类型单一
print(check_password_strength("Abc123!")) # 强:包含大小写字母、数字和特殊字符
# 案例3:解析CSV格式数据
def parse_csv_line(line):
"""解析CSV格式的一行数据"""
return [field.strip() for field in line.split(',')]
# 案例4:统计单词频率
def word_frequency(text):
"""统计文本中单词频率"""
words = text.lower().split()
frequency = {}
for word in words:
word = word.strip('.,!?;:')
if word:
frequency[word] = frequency.get(word, 0) + 1
return frequency
# 测试
csv_line = "Alice, 25, Engineer, New York"
print(parse_csv_line(csv_line)) # ['Alice', '25', 'Engineer', 'New York']
text = "Hello world! Hello Python. Python is great!"
freq = word_frequency(text)
print(freq) # {'hello': 2, 'world': 1, 'python': 2, 'is': 1, 'great': 1}
完成以下基础字符串操作:
实现以下进阶功能:
选择以下项目之一进行开发:
创建一个文本处理工具,支持以下功能:
实现一个密码管理器,包含:
开发一个简单的数据分析工具: