当前位置: 首页 > news >正文

python中.format() 方法

.format() 方法是一种用于格式化字符串的方法,它允许将变量的值插入到字符串中的占位符位置上。该方法可以接受一个或多个参数,并根据给定的格式规则将它们插入到字符串中。

下面是一些使用 .format() 方法的示例:

# 基本用法
name = "Alice"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
# 输出: My name is Alice and I am 25 years old.# 指定参数顺序
print("My name is {0} and I am {1} years old.".format(name, age))
# 输出: My name is Alice and I am 25 years old.# 使用关键字参数
print("My name is {name} and I am {age} years old.".format(name=name, age=age))
# 输出: My name is Alice and I am 25 years old.# 指定数值格式
pi = 3.14159
print("The value of pi is {:.2f}.".format(pi))
# 输出: The value of pi is 3.14.# 使用索引和属性
class Person:def __init__(self, name, age):self.name = nameself.age = ageperson = Person("Bob", 30)
print("My name is {0.name} and I am {0.age} years old.".format(person))
# 输出: My name is Bob and I am 30 years old.

在上述示例中,.format() 方法使用花括号 {} 来表示占位符,其中可以包含参数索引、关键字或属性。可以在占位符中使用冒号 : 指定格式规则,例如精度、宽度等。

需要注意的是,在 Python 3.6 及更高版本中,还可以使用 f-strings 来实现相同的字符串格式化功能,它使用更简洁的语法,如下所示:

print(f"My name is {name} and I am {age} years old.")
# 输出: My name is Alice and I am 25 years old.

.format() 方法仍然是一个有用的方法,特别是在不支持 f-strings 的旧版本的 Python 中。

格式说明符

格式说明符是一种用于控制字符串输出格式的占位符。它们以一对花括号 {} 表示,可以在字符串中嵌入变量、常量或表达式,并指定它们的格式。在 Python 中,常用的格式说明符有:

  • %d:将整数表示为十进制数。
  • %f:将浮点数表示为小数形式。
  • %s:将字符串表示为字符串形式。
  • %r:将对象表示为 repr() 形式。
  • %.nf:在浮点数的格式说明符中,n 表示小数点后保留的位数。
  • %x:将整数表示为十六进制数。

以下是一些示例:

age = 25
name = "Alice"
pi = 3.14159print("My age is %d" % age)  # 输出 My age is 25
print("My name is %s" % name)  # 输出 My name is Alice
print("The value of pi is approximately %.2f" % pi)  # 输出 The value of pi is approximately 3.14print("My name is {!r}".format(name))  # 输出 My name is 'Alice'
print("The value of pi is approximately {:.2f}".format(pi))  # 输出 The value of pi is approximately 3.14print(f"My age is {age}")  # 使用 f-string 输出 My age is 25
print(f"My name is {name!r}")  # 使用 f-string 输出 My name is 'Alice'

在上述示例中,我们使用了不同的格式说明符来格式化输出。使用 % 运算符进行格式化是一种旧的方法,而使用 .format() 方法或 f-string 是 Python3 中更推荐的方法。

需要注意的是,格式说明符只是一种用于格式化输出的工具,不会修改变量本身的值。

repr() 形式

repr() 是一个内置函数,它可以将对象转换为一个供解释器读取的形式。它返回的字符串通常可以通过 eval() 函数重新生成该对象。

每个对象都可以有不同的 repr() 形式,取决于对象的类型和实现。通常,repr() 的返回值应该是一个以字符串形式表示的对象,其中包含有关对象的信息,以便于调试和查看。

以下是一些常见的示例,展示了不同类型对象的 repr() 形式:

# 字符串
s = "Hello, world!"
print(repr(s))  # 输出 'Hello, world!'# 整数
n = 42
print(repr(n))  # 输出 42# 列表
lst = [1, 2, 3]
print(repr(lst))  # 输出 [1, 2, 3]# 字典
d = {'a': 1, 'b': 2}
print(repr(d))  # 输出 {'a': 1, 'b': 2}# 自定义类
class Point:def __init__(self, x, y):self.x = xself.y = ydef __repr__(self):return f"Point({self.x}, {self.y})"p = Point(2, 3)
print(repr(p))  # 输出 Point(2, 3)

在上述示例中,每个对象的 repr() 方法返回了一个代表该对象的字符串表示形式。需要注意的是,自定义类可以通过实现 __repr__() 方法来定义自己的 repr() 形式。这使得我们可以根据需要自定义对象在调试输出中的显示方式。

http://www.lryc.cn/news/253059.html

相关文章:

  • 【新手解答8】深入探索 C 语言:递归与循环的应用
  • 服务器中深度学习环境的配置
  • html实现各种好看的鼠标滑过图片特效模板
  • leetcode:LCR 122. 路径加密python3解法)
  • vue中实现纯数字键盘
  • C#简化工作之实现网页爬虫获取数据
  • 回顾过去的五年
  • 企业微信http协议接口调用,根据手机号搜索联系人
  • 第三方支付原理
  • logcat日志的使用——Qt For Android
  • 软著项目推荐 深度学习的智能中文对话问答机器人
  • 灰度发布专题---3、Nginx+Lua灰度发布
  • 冬天来了,波司登的高端化“春天”不远了?
  • Vue3.0优点详解
  • Unity3D URP 自定义范围的特效热扭曲详解
  • Apache Flink(一):Apache Flink是什么?
  • Wordpress自动定时发布怎么开通-Wordpress怎么自动发布原创文章
  • VUE项目中问题学习总结(一)
  • 使用K-means把人群分类
  • 静态HTTP和动态HTTP有什么区别
  • 分享66个在线客服JS特效,总有一款适合您
  • Backend - Django JsonResponse HttpResponse
  • 第四阶|自在行草 暄桐教室,林曦书法 从书法之美到生活之美
  • kubernetes详解——从入门到入土(更新中~)
  • VScode异常处理 (因为在此系统上禁止运行脚本)
  • (5h)Unity3D快速入门之Roll-A-Ball游戏开发
  • 分享86个选项卡TABJS特效,总有一款适合您
  • 【Linux】Linux基础
  • 动态规划求解 fibonacci 数列
  • js最大公约数的实现有哪些办法