python从零开始--04print和input

本文介绍了Python中使用print函数进行格式化输出的方法,包括不同类型的占位符使用方式,并展示了如何利用input函数获取用户输入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  print功能用来打印字符,有好几种写法;下面是目前最为推荐的写法,是使用format方法格式化字符串:

print("xxxxx{}, yyyyy{}".format(a, b))。

my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'

print("Let's talk about {}".format(my_name ))
print("He's {} inches tall.".format(my_height))
print("He's {} pounds heavy.".format(my_weight))
print("Acutally that's not too heavy.")
print("He's got {} eyes and {}.".format(my_eyes,my_hair))
print("His teeth are usually {} depending on the coffee".format(my_teeth))

# this line is tricky, try to get it exactly right
total = my_age + my_height + my_weight
print("If I add {},{}, and {} I get {}".format(my_age,my_height,my_weight,total))

 

占位符替换内容
%d整数
%f浮点数
%s字符串
%x十六进制整数
   
print("This is {} value {:.1f}".format('PI', 3.1415926))
print('{0}的体重为{1:.2f}'.format('阿牛', 99.786))
This is PI value 3.1
阿牛的体重为99.79

   输入用input: 

var_input = input()
print(var_input)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值