鱼C论坛

 找回密码
 立即注册
查看: 1620|回复: 7

字符串和整型变量的值如何同时print

[复制链接]
发表于 2018-2-1 10:24:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
print(".... How many seconds in a year?....")
daysperyear=365
hoursperday=24
minutesperhour=60
secondsperminutes=60
total=daysperyear*hoursperday*minutesperhour*secondsperminutes
print(total)
print("There are" total "seconds in a year")
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-2-1 10:27:28 | 显示全部楼层
a='abc'
b=123
print('a是%s,b是%d'%(a,b))或print(a,b)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-1 10:28:05 | 显示全部楼层
print("There are", 123, "seconds in a year")
print("There are " + str(123) + " seconds in a year")
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-1 10:46:48 | 显示全部楼层
print("There are %s seconds in a year"% total )
%s 是格式化成字符串的意思。 %是格式化的开始,% total 把total转化为字符串到%s的位置。
输出结果就是 There are 31536000 seconds in a year
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-1 10:58:30 | 显示全部楼层
print(".... How many seconds in a year?....")
daysperyear=365
hoursperday=24
minutesperhour=60
secondsperminutes=60
total=daysperyear*hoursperday*minutesperhour*secondsperminutes
print(total)
print("There are" ,total ,"seconds in a year")
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-1 10:59:10 | 显示全部楼层
饭特熙 发表于 2018-2-1 10:58
print(".... How many seconds in a year?....")
daysperyear=365
hoursperday=24

total前后各加一个逗号
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-1 11:11:40 | 显示全部楼层
有以下几种方法:
1 将整形转换为str再拼接,比如
  1. print('good'+str(123))
复制代码

2 用逗号将两种类型分开,比如
  1. print('good',123)
复制代码

3 用格式化输出方式,比如
  1. print('good%d'%123)
复制代码

或者
  1. print('good{0}'.format(123))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-1 11:34:56 | 显示全部楼层
print(".... How many seconds in a year?....")
daysperyear=365
hoursperday=24
minutesperhour=60
secondsperminutes=60
total=daysperyear*hoursperday*minutesperhour*secondsperminutes
print(total)
print("There are",total,"seconds in a year") #用,分割法,缺点是增加了两个空格
print("There are"+str(total)+"seconds in a year")#将数字用str转化成字符串,用+号连接字符法
print("There are%dseconds in a year"%total)  #用%格式化方法,具体用法参考百度字符串格式化方法。
print("There are{}seconds in a year".format(total))#用{}跟.format格式化方法
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-27 16:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表