python | 输入输出input print

复习,mark一下~

python | 输入输出input print

输入 input

input赛高,直接放代码
输入的结果都是str类型,如果希望输入数字,需要类型转换

user_name = input("Please enter your name ")
print("Your name in all capitals is",user_name.upper(),
"and has length", len(user_name))

user_radius = input("Please enter the radius of the circle ")
radius = float(user_radius)
diameter = 2*radius

输出 print

通过代码学~

>>> print("Hello")
Hello
>>> print("Hello","World")
Hello World
>>> print("Hello","World", sep=" *** ")
Hello *** World
>>> print("Hello","World", end=" *** ")
Hello World ***
>>> print("Hello", end=" *** "); print("World")
Hello *** World

进阶一步,使用%
String Formatting Conversion Characters

CharacterOutput Format
d , iInteger
uUnsigned Integer
fFloating point as m.ddddd
eFloating point as m.ddddde+/-xx
EFloating point as m.dddddE+/-xx
gUse %e for exponents less than −4 or greater than +5, otherwise us %f
cSingle character
sString, or any Python data object that can be converted to a string by using the str function
%Insert a literal % character
print(name, "is", age, "years old.")
print("%s is %d years old." % (name, age))

%再进一步:
Additional formatting options

ModifierExampleDescription
number%20dPut the value in a field width of 20
-%-20dPut the value in a field 20 characters wide, left-justified
+%+20dPut the value in a field 20 characters wide, right-justified
0%020dPut the value in a field 20 characters wide, fill in with leading zeros
.%20.2fPut the value in a field 20 characters wide with 2 characters to the right of the decimal point.
(name)%(name)dGet the value from the supplied dictionary using name as the key.
>>> price = 24
>>> item = "banana"
>>> print("The %s costs %d cents"%(item,price))
The banana costs 24 cents
>>> print("The %+10s costs %5.2f cents"%(item,price))
The banana costs 24.00 cents
>>> print("The %+10s costs %10.2f cents"%(item,price))
The banana costs 24.00 cents
>>> item_dict = {"item":"banana","cost":24}
>>> print("The %(item)s costs %(cost)7.1f cents"%item_dict)
The banana costs 24.0 cents

再进一步,format赛高:
直接代码例子

>>> a,b,c,d = 1,2,3,4
>>> print('a:{},b:{},c:{},d:{}'.format(a,b,c,d))
a:1,b:2,c:3,d:4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值