format()函数的用法

Python中的format()函数用于格式化字符串。它可以将不同类型的数据格式化为字符串中指定的格式。以下是format()函数的各种用法及示例:

"{<参数序号>:<格式控制标记>}".format()

格式内容:

<.精度>-后面可以加<类型>

<类型>

整数类型:b--2进制、c--Unicode、d--十进制、o--八进制、x/X--十六进制

浮点数类型:

  • e/E--以科学计数法的形式输出浮点数
  • f--以浮点数的形式输出浮点数。默认情况下,会输出小数点后面六位数字,但是可以通过指定精度来控制输出的位数
  • %:--以百分数的形式输出浮点数,乘以100并在末尾加上百分号。
  • s--输出字符串
  • r--输出字符串的repr()形式(原始字符串)

1、位置参数

这是最基本的用法,可以将格式化字符串中的占位符{}替换为相应的位置参数。

name = "John"
age = 30
print("My name is {} and I am {} years old.".format(name, age))
# 输出:My name is John and I am 30 years old.

2、命名参数

可以使用命名参数来指定占位符的值,这样可以使代码更易读

print("My name is {name} and I am {age} years old.".format(name="John", age=30))
# 输出:My name is John and I am 30 years old.

3、位置参数与命名参数的混合使用

位置参数和命名参数可以混合使用,但是位置参数必须在前面。

print("My name is {0} and I am {1} years old. My hobby is {hobby}.".format("John", 30, hobby="swimming"))
# 输出:My name is John and I am 30 years old. My hobby is swimming.

4、使用字典格式化字符串

可以使用字典的键值对来格式化字符串,使用{}包含键名,键名在format()函数中指定。

person = {'name': 'John', 'age': 30}
print("My name is {name} and I am {age} years old.".format(**person))
# 输出:My name is John and I am 30 years old.

5、格式化数字

可以使用占位符指定数字的格式,例如{:d}表示整数,{:f}表示浮点数。

num = 3.14159
print("The value of pi is approximately {:.2f}.".format(num))
# 输出:The value of pi is approximately 3.14.

6、格式化时间

可以使用strftime()函数指定时间的格式,然后在格式化字符串中使用占位符替换时间。

from datetime import datetime
now = datetime.now()
print("Today is {:%Y-%m-%d %H:%M:%S}.".format(now))
# 输出:Today is 2023-04-19 18:30:00.

7、使用格式字符串语法

Python 3.6及以上版本支持使用f-strings(格式化字符串字面值)来格式化字符串,用法比较简单,直接在字符串前面加上字母“f”,然后使用花括号{}包含变量名。

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

8、格式化二进制、八进制和十六进制数字

可以使用占位符指定数字的进制,例如{:b}表示二进制,{:o}表示八进制,{:x}表示十六进制。

num = 42
print("The binary representation of 42 is {:b}.".format(num))
# 输出:The binary representation of 42 is 101010.

print("The octal representation of 42 is {:o}.".format(num))
# 输出:The octal representation of 42 is 52.

print("The hexadecimal representation of 42 is {:x}.".format(num))
# 输出:The hexadecimal representation of 42 is 2a.

9.格式化货币

可以使用占位符指定货币符号和小数点的位置,例如{:,.2f}表示保留两位小数,以逗号分隔千位,使用美元符号。

price = 123456.789
print("The price is ${:,.2f}.".format(price))
# 输出:The price is $123,456.79.

10、格式化百分比

可以使用占位符将小数转换为百分比,例如{:.2%}表示保留两位小数,乘以100并添加百分号。

ratio = 0.5678
print("The ratio is {:.2%}.".format(ratio))
# 输出:The ratio is 56.78%.

11、当需要控制字符串的宽度时,可以使用占位符指定输出的字符宽度

name = "Alice"
print("Hello, {:10}!".format(name))
# 输出:Hello, Alice     !

print("Hello, {:^10}!".format(name))
# 输出:Hello,   Alice   !

print("Hello, {:<10}!".format(name))
# 输出:Hello, Alice     !

print("Hello, {:>10}!".format(name))
# 输出:Hello,      Alice!

12、如果要将字符长度作为变量传递给格式化字符串的宽度字段,可以使用f-string(格式化字符串字面值)。

width = 10
i = 42
formatted_string = f"{i:^{width}}"
print(formatted_string)

或者

width = 10
i = 42
formatted_string = "{:^{}}".format(i, width)
print(formatted_string)

  • 8
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值