第6章 字符串操作

目录

  • [1. 处理字符串](#1. 处理字符串)
    • [1.1 字符串字面量](#1.1 字符串字面量)
      • [1.1.1 双引号](#1.1.1 双引号)
      • [1.1.2 转义字符](#1.1.2 转义字符)
      • [1.1.3 原始字符串](#1.1.3 原始字符串)
      • [1.1.4 用三重引号的多行字符串](#1.1.4 用三重引号的多行字符串)
    • [1.2 字符串索引](#1.2 字符串索引)
    • [1.3 字符串切片](#1.3 字符串切片)
    • [1.4 字符串的 in 和 not in 操作符](#1.4 字符串的 in 和 not in 操作符)
  • [2. 将字符串放入其他字符串](#2. 将字符串放入其他字符串)
  • [3. 有用的字符串方法](#3. 有用的字符串方法)
    • [3.1 upper() 方法](#3.1 upper() 方法)
    • [3.2 lower()方法](#3.2 lower()方法)
    • [3.3 isupper() 方法](#3.3 isupper() 方法)
    • [3.4 islower() 方法](#3.4 islower() 方法)
    • [3.5 isX() 字符串方法](#3.5 isX() 字符串方法)
    • [3.6 startwith() 和 endwith() 字符串方法](#3.6 startwith() 和 endwith() 字符串方法)
    • [3.7 join() 字符串方法](#3.7 join() 字符串方法)
    • [3.8 split() 字符串方法分隔字符串](#3.8 split() 字符串方法分隔字符串)
    • [3.9 使用 partition() 方法分隔字符串](#3.9 使用 partition() 方法分隔字符串)
    • [3.10 用 rjust() 、ljust() 和 center() 方法对齐文本](#3.10 用 rjust() 、ljust() 和 center() 方法对齐文本)
    • [3.11 用 strip() 、rstrip() 和 lstrip() 方法删除空白字符](#3.11 用 strip() 、rstrip() 和 lstrip() 方法删除空白字符)
  • [4. 使用 ord() 和 chr() 函数的字符的数值](#4. 使用 ord() 和 chr() 函数的字符的数值)

1. 处理字符串

1.1 字符串字面量

1.1.1 双引号

python 复制代码
str = "ABC"

1.1.2 转义字符

转义字符
'
"
\n
\t
\

1.1.3 原始字符串

  • 原始字符串:忽略转义字符
python 复制代码
str = r"ABC\n"
print(str)

1.1.4 用三重引号的多行字符串

  • 字符串与书写格式完全相同
python 复制代码
str = ''' abc
 a 1 \n
 b 2 \n
 c 3 \n
 123'''

print(str)

1.2 字符串索引

python 复制代码
message = "ABC"
print(message[1])

1.3 字符串切片

python 复制代码
message = "ABC"
print(message[0:2])

1.4 字符串的 in 和 not in 操作符

python 复制代码
message = "ABC"
print("A" in message)
print("A" not in message)

2. 将字符串放入其他字符串

python 复制代码
A = "A"
B = "B"

print(A + ": " + B)
print("%s: %s" % (A, B))
print("{}: {}".format(A, B))
print(f"{A}: {B}")

3. 有用的字符串方法

3.1 upper() 方法

python 复制代码
message = "abc"
print(message.upper())

3.2 lower()方法

python 复制代码
message = "ABC"
print(message.lower())

3.3 isupper() 方法

python 复制代码
message = "aBC"
print(message.isupper())

3.4 islower() 方法

python 复制代码
message = "aBc"
print(message.islower())

3.5 isX() 字符串方法

字符串方法 解释
isalpha() 是否只包含字母
isalnum() 是否只包含字母和数字
isdecimal() 是否只包含数字
isspace() 是否只包含空白(制表符、换行符、空格)
istitle() 是否以大写字母开头

3.6 startwith() 和 endwith() 字符串方法

python 复制代码
message = "ABC"
print(message.startswith("AB"))
print(message.endswith("BC"))

3.7 join() 字符串方法

python 复制代码
message = "ABC"
print(message.join(["1", "2", "3"]))

3.8 split() 字符串方法分隔字符串

  • 默认以空白(制表符、换行符、空格)作为分隔符
  • 默认全部分隔
python 复制代码
message = "ABCABC"
print(message.split("B", 1))

3.9 使用 partition() 方法分隔字符串

python 复制代码
message = "ABCABC"
print(message.partition("B"))

3.10 用 rjust() 、ljust() 和 center() 方法对齐文本

python 复制代码
message = "ABC"
print(message.rjust(10, "~"))
# ~~~~~~~ABC
print(message.ljust(10, "~"))
# ABC~~~~~~~
print(message.center(10, "~"))
# ~~~ABC~~~~

3.11 用 strip() 、rstrip() 和 lstrip() 方法删除空白字符

python 复制代码
message = "  ABC\n"
print(message.rstrip())
#   ABC
print(message.lstrip())
# ABC
#
print(message.strip())
# ABC

4. 使用 ord() 和 chr() 函数的字符的数值

python 复制代码
print(ord("A"))
# 65
print(chr(65))
# A
相关推荐
孟健11 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞13 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽15 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程20 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪20 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook20 小时前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田1 天前
使用 pkgutil 实现动态插件系统
python
前端付豪2 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽2 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战2 天前
Pydantic配置管理最佳实践(一)
python