网站首页 > 基础教程 正文
什么是字符串?
字符串是用单引号、双引号或三引号括起来的字符序列。
# 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-31 Python办公神器:教你如何快速分拆、删页、合并PDF文件
- 2024-10-31 Python3中的字符串操作 python3 字符串操作
- 2024-10-31 学好了Python,我们就可以玩转字符串算法了
- 2024-10-31 玩转Python—字符串使用教程 python字符串常用方法
- 2024-10-31 Python3中可能不会用到的10个功能!但是能让你的代码更简洁直观
- 2024-10-31 python作业(三) python编程作业
- 2024-10-31 手把手教你写爬虫 |Python 采集大众点评数据采集实战
- 2024-10-31 「python杂谈」使用多个分隔符分隔字符串
- 2024-10-31 如何用Python+OpenCV处理图像色彩?终于有人讲明白了
- 2024-10-31 字符串基本操作2-2-Python3零基础入门
- 最近发表
- 标签列表
-
- jsp (69)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- pythonif (86)
- location.href (69)
- dockerexec (65)
- tail-f (79)
- queryselectorall (63)
- location.search (79)
- bootstrap教程 (74)
- 单例 (62)
- linuxgzip (68)
- 字符串连接 (73)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)