网站首页 > 技术文章 正文
什么是字符串?
字符串是用单引号、双引号或三引号括起来的字符序列。
# Single quotes
string1 = 'Hello, World!'
# Double quotes
string2 = "Hello, World!"
# Triple quotes (useful for multi-line strings)
string3 = '''Hello,
World!'''
访问字符串中的字符
可以使用索引访问字符串中的单个字符。索引从 0 开始。
greeting = "Hello, World!"
print(greeting[0]) # Output: H
print(greeting[7]) # Output: W
切片字符串
切片允许您通过指定开始和结束索引从字符串中获取子字符串。
greeting = "Hello, World!"
print(greeting[0:5]) # Output: Hello
print(greeting[7:12]) # Output: World
还可以使用负索引从字符串的末尾进行切片。
print(greeting[-6:]) # Output: World!
String 方法
Python 提供了几种用于处理字符串的内置方法。
len()
该 len() 函数返回字符串的长度。
print(len(greeting)) # Output: 13
lower()和upper()
该 lower() 方法将字符串转换为小写,该 upper() 方法将字符串转换为大写。
print(greeting.lower()) # Output: hello, world!
print(greeting.upper()) # Output: HELLO, WORLD!
strip()
该 strip() 方法从字符串中删除任何前导和尾随空格。
whitespace_str = " Hello, World! "
print(whitespace_str.strip()) # Output: Hello, World!
replace()
该 replace() 方法将子字符串的所有匹配项替换为另一个子字符串。
print(greeting.replace("World", "Python")) # Output: Hello, Python!
split()
该 split() 方法根据指定的分隔符将字符串拆分为子字符串列表。
print(greeting.split(",")) # Output: ['Hello', ' World!']
join()
该 join() 方法将字符串列表联接到单个字符串中,每个元素由指定的分隔符分隔。
words = ["Hello", "World"]
print(" ".join(words)) # Output: Hello World
字符串格式
字符串格式允许您创建具有动态内容的字符串。
用format()
该 format() 方法将字符串中的占位符替换为指定的值。
name = "Alice"
age = 25
formatted_string = "My name is {} and I am {} years old.".format(name, age)
print(formatted_string) # Output: My name is Alice and I am 25 years old.
使用 f 字符串
f-Strings(格式化字符串文本)提供了一种更简洁的字符串格式设置方法。它们在 Python 3.6 及更高版本中可用。
formatted_string = f"My name is {name} and I am {age} years old."
print(formatted_string) # Output: My name is Alice and I am 25 years old.
转义字符
若要在字符串中包含特殊字符,可以使用转义字符,前面有一个反斜杠 \ 。
# Including quotes in a string
quote = "He said, \"Python is awesome!\""
print(quote) # Output: He said, "Python is awesome!"
# Including a backslash in a string
path = "C:\\Users\\Alice"
print(path) # Output: C:\Users\Alice
多行字符串
可以使用三引号创建多行字符串。
multi_line_string = """This is a
multi-line
string."""
print(multi_line_string)
# Output:
# This is a
# multi-line
# string.
猜你喜欢
- 2024-10-05 Python基础知识——字符串(python字符串的常用方法)
- 2024-10-05 javascript内置对象总结 - String
- 2024-10-05 Python 中字符串和字符串处理的综合指南
- 2024-10-05 C++中的string用法(c++ string用法总结)
- 2024-10-05 Excel文本函数——CONCATENATE函数详解
- 2024-10-05 探究C语言中字符串大小和长度易错点
- 2024-10-05 【PythonTip题库300题】第47题:字符串中缺失的字母
- 2024-10-05 Rust: 字符串总结,虽常见但非常重要
- 2024-10-05 这些字符串处理函数你用过吗(字符串常用操作函数)
- 2024-10-05 C字符串搜索和替换算法(字符串查找替换c语言)
- 08-03MySQL数据库的预处理详解
- 08-03《阿常·MySQL 70讲》全套教学视频
- 08-03隐式等待、显示等待和强制等待
- 08-03零基础C#上位机框架项目实例(完结篇)
- 08-03一文搞懂构建Web内容的技术
- 08-03西门子WINCC中的VBScript(VBS)常用于自动化脚本开发
- 08-03力控和sql2000之间的数据转储
- 08-03组态王|通过日历控件选择时间段查询历史报警
- 1521℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 623℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 526℃MySQL service启动脚本浅析(r12笔记第59天)
- 492℃启用MySQL查询缓存(mysql8.0查询缓存)
- 491℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 479℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 460℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 458℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- windowsscripthost (69)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- htmlbackground-image (68)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- asynccallback (71)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)