第1天:Python基础语法(五)

正文:

在之前的文章中,我们已经学习了Python的基本语法集合和集合的一些常用操作。

在本篇文章中,我们将继续学习其他类型

字符串格式化

使用操作符%s来实现

➢ 几个%s就几个变量

➢ 超过一个变量时,需要用元组%()来获取,且顺序不可换

方法一:

python 复制代码
a = 'name %s,age %s' %('大卫',20)
print(a)
#输出结果:name 大卫,age 20

方法二:

python 复制代码
a = 'name %s,age %s'
name = 'dawei'
age ='20'
print(a % (name,age))

格式化函数-format

➢ string.format函数用来格式化字符串

➢ 使用format的字符串主体使用大括号{}来替代格式符string.format(data, data, data...)

python 复制代码
a = 'name {},age {}'
name = 'dawei'
age ='20'
print(a.format(name,age))

Python3.6加入的新格式化方案f-strings

➢ 定义一个变量

➢ 字符串前加f符号

➢ 需要格式化的位置使用{变量名},需提前定义变量

python 复制代码
name_01 = 'dawei'
age_01 ='20'
a = f'my name {name_01},my age {age_01}'   #加f
print(a)

常用格式化符号

python中各种转义字符

Python运算符

算数运算符

比较运算符

赋值运算符

位运算符

逻辑运算符

运算符优先级

相关推荐
曲幽1 小时前
FastAPI流式输出实战与避坑指南:让AI像人一样“边想边说”
python·ai·fastapi·web·stream·chat·async·generator·ollama
Flittly1 小时前
【从零手写 AI Agent:learn-claude-code 项目实战笔记】(1)The Agent Loop (智能体循环)
python·agent
vivo互联网技术3 小时前
ICLR2026 | 视频虚化新突破!Any-to-Bokeh 一键生成电影感连贯效果
人工智能·python·深度学习
敏编程4 小时前
一天一个Python库:virtualenv - 隔离你的Python环境,保持项目整洁
python
喝茶与编码6 小时前
Python异步并发控制:asyncio.gather 与 Semaphore 协同设计解析
后端·python
zone77396 小时前
003:RAG 入门-LangChain 读取图片数据
后端·python·面试
用户8356290780516 小时前
在 PowerPoint 中用 Python 添加和定制形状的完整教程
后端·python
用户962377954487 小时前
🚀 docx2md-picgo:Word 文档图片一键上传图床工具
python·markdown
zone77391 天前
001:简单 RAG 入门
后端·python·面试