使用+
运算符将多个字符串连接起来。例如:
s1 = "Hello"
s2 = "World"
result = s1 + " " + s2
print(result) # 输出:Hello World
使用*
运算符重复字符串。例如:
s = "abc"
result = s * 3
print(result) # 输出:abcabcabc
使用len()
函数获取字符串长度。例如:
s = "Python"
length = len(s)
print(length) # 输出:6
通过索引访问单个字符。例如:
s = "Python"
char = s[2]
print(char) # 输出:t
使用切片获取子字符串。例如:
s = "Python"
substring = s[1:4]
print(substring) # 输出:yth
使用f-string进行格式化。例如:
name = "Alice"
age = 25
result = f"My name is {name} and I am {age} years old."
print(result) 输出:My name is Alice and I am 25 years old
使用format()
方法进行格式化。例如:
name = "Alice"
age = 25
result = "My name is {} and I am {} years old.".format(name, age)
print(result) 输出:My name is Alice and I am 25 years old
使用%
运算符进行格式化。例如:
name = "Alice"
age = 25
result = "My name is %s and I am %d years old." % (name, age)
print(result) 输出:My name is Alice and I am 25 years old
使用upper()
和lower()
方法转换大小写。例如:
s = "Python"
upper_case = s.upper()
lower_case = s.lower()
print(upper_case) # 输出:PYTHON
print(lower_case) # 输出:python
使用capitalize()
方法将首字母大写。例如:
s = "hello world"
result = s.capitalize()
print(result) # 输出:Hello world
使用title()
方法将每个单词首字母大写。例如:
s = "hello world"
result = s.title()
print(result) # 输出:Hello World
使用replace()
方法替换子字符串。例如:
s = "Hello World"
result = s.replace("World", "Python")
print(result) # 输出:Hello Python
使用split()
方法分割字符串。例如:
s = "Hello,World,Python"
parts = s.split(",")
print(parts) # 输出:['Hello', 'World', 'Python']
使用join()
方法合并字符串列表。例如:
parts = ["Hello", "World", "Python"]
result = ",".join(parts)
print(result) # 输出:Hello,World,Python
使用strip()
方法去除两端空白。例如:
s = " Python "
result = s.strip()
print(result) 输出:Python
使用lstrip()
方法去除左端空白。例如:
s = " Python "
result = s.lstrip()
print(result) 输出:Python
使用rstrip()
方法去除右端空白。例如:
s = " Python "
result = s.rstrip()
print(result) 输出: Python
使用find()
方法查找子串位置。例如:
s = "Python"
index = s.find("th")
print(index) # 输出:2
使用startswith()
方法检查前缀。例如:
s = "Python"
result = s.startswith("Py")
print(result) # 输出:True
使用endswith()
方法检查后缀。例如:
s = "Python"
result = s.endswith("on")
print(result) # 输出:True
使用count()
方法统计子串出现次数。例如:
s = "Python programming is fun"
count = s.count("m")
print(count) # 输出:2
使用isalpha()
、isdigit()
等方法检查字符类型。例如:
s = "Python"
# s.isalpha():该方法用于判断字符串中的所有字符是否全为字母。
result = s.isalpha()
print(result) # 输出:True
# s.isdigit():此方法用于判断字符串中的所有字符是否全为数字。
result = s.isdigit()
print(result) # 输出:False
使用ljust()
、rjust()
、center()
方法对齐字符串。例如:
s = "Python"
# center 居中对齐
result = s.center(10, "*")
print(result) # 输出:**Python**
# ljust 居左对齐
result = s.ljust(10, "*")
print(result) # 输出:Python****
# rjust 居右对齐
result = s.rjust(10, "*")
print(result) # 输出:****Python
使用encode()
方法转换编码。例如:
s = "Python"
encoded = s.encode("utf-8")
使用decode()
方法解码字节串。例如:
bytes_data = b"Python"
decoded = bytes_data.decode("utf-8")
使用转义字符处理特殊字符。例如:
s = "Line 1\nLine 2"
使用r
前缀保持原始格式。例如:
s = r"C:\Users\Name"
使用in
运算符检查包含关系。例如:
s = "Python"
result = "th" in s
print(result) # 输出:True
使用比较运算符比较字符串。例如:
s1 = "Apple"
s2 = "Banana"
result = s1 < s2
print(result) # 输出:True
使用切片反转字符串。例如:
s = "Python"
reversed_str = s[::-1]
print(reversed_str) # 输出:nohtyP
使用zfill()
方法填充数字0字符串。例如:
s = "42"
result = s.zfill(5)
print(result) # 输出:00042
使用splitlines()
方法分割多行字符串。例如:
s = "Line 1\nLine 2\nLine 3"
lines = s.splitlines()