# 示例代码:Python字符串的基本操作
# 创建字符串
string1 = "Hello, World!"
print("创建的字符串:", string1)
# 字符串长度
length = len(string1)
print("字符串长度:", length)
# 字符串拼接
string2 = " Welcome to Python"
combined_string = string1 + string2
print("拼接后的字符串:", combined_string)
# 字符串切片
substring = string1[0:5]
print("切片后的子字符串:", substring)
# 字符串查找
index = string1.find("World")
print("查找 'World' 的索引:", index)
# 字符串替换
replaced_string = string1.replace("World", "Python")
print("替换后的字符串:", replaced_string)
# 字符串分割
split_strings = string1.split(", ")
print("分割后的字符串列表:", split_strings)
# 字符串格式化
name = "Alice"
formatted_string = f"Hello, {name}!"
print("格式化后的字符串:", formatted_string)
len()
函数获取字符串的长度。+
运算符将两个字符串连接在一起。[start:end]
语法从字符串中提取子字符串。find()
方法查找子字符串的位置,返回其索引。replace()
方法将字符串中的某个子字符串替换为另一个子字符串。split()
方法根据指定的分隔符将字符串分割成列表。上一篇:print在python中用法
下一篇:python3.10
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站