Python中字符串的操作方法

字符串拼接

使用+运算符将多个字符串连接起来。例如:

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)

使用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方法)

使用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()

你可能感兴趣的:(python,java,开发语言)