Python爬虫日记- format格式以及f表达式

本文详细介绍了Python中的字符串格式化方法,包括无指定的format、有指定的format以及关键词参数的使用方式。此外,还讲解了f表达式的用法,展示了如何在字符串中直接插入变量并进行格式化,包括浮点数的精度控制。通过实例演示,帮助读者理解这些格式化技术的应用。
摘要由CSDN通过智能技术生成

Format 使用方法

  1. 无指定的format
print('{} {}'.format('hello','world'))  
#hello放在第一个空格{}, world放在第二个空格{}
#[hello world]
  1. 有指定的format
print('{0} {1}'.format('hello','world')) 
#0代表format后面的第一个字符,1代表第二个字符
# [hello world]
print('{0} {1} {0}'.format('hello','world'))
#记住,format括号里面的字符中,hello代表0,world代表1 
#所以print出来的是 
# [hello world hello]
  1. 用关键词来指定format里面的字符
print('{a} {tom} {a}'.format(tom = 'hello', a = 'world'))  
#output为
# 【world hello world】

f表达式 – f可以解释任意的数据类型

name = '张三'
age = 18
print(f"名字: {name} 年龄: {age: .2f}")
#output
#【名字: 张三 年龄:  18.00】
rf_scores= model()
print(f'Linear Regression average RMSE: {rf_scores.mean():.4f}')
# :.4f代表着解析的数字round4位
#output为
#【Linear Regression average RMSE: 0.6102】
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jianafeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值