1.1输入
? ? ? ? 我们说input()函数直接获取信息,究竟有什么意义呢?意思就是,用户输入的任何内容都将按照它实际的内容保存,并且Python将尝试为为输出的内容匹配正确的数据类型。如果用户输人5,那么这个数将值保存为整数。如果用户输入5.0,该值将保存为浮点数。
? ? ? ? 如下:
? ? ? ? ? ? ? ? 输入:
a = input()
b = input()
print(a, b)
????????????????输出:
5
5.0
5 5.0
Process finished with exit code 0
但是,解释器读到的一定是字符串类型的,如果你想要使用其他数据类型,需要类型转换。
?类型转换:
num = input()
print(num, type(num))
num1 = int(num)
print(num==5)
print(num1==5)
运行:
5
5 <class 'str'>
False
True
Process finished with exit code 0
1.2输出
? ? ? ? 很多时候,我们想把一个(或多个)变量中的值放到一个更大的字符串中。我们知道可以把字符串连接起来成为一个新的字符串。当字符串变的较长的时候,这可能变的难以处理,并且如果我们忘记在变量周围添加一些额外的空格,这很容易其格式化输出很难看。
我们Ctrl+鼠标右键点击print后可以看到:
print(*args, sep=' ', end='\n', file)
*args: arguments 参数
sep: separator 分隔符,?用来间隔多个对象。
end: end 结尾,用来设定以什么结尾。默认值是换行符 \n,我们可以换成其他字符。
file:要写入的文件对象。
print("Hello World") #字符串类型可以直接输出
---------------------------------------------
运行结果如下:
Hello World
-----------------------------------------------
a=1
b="Hello World"
print(a, b) #可以一次输出多个对象,对象之间用逗号分隔
------------------------------------------------
运行结果如下:
1 Hello World
-------------------------------------------------
#如果直接输出字符串,而不是用对象表示的话,可以不使用逗号
print("sun""haoyu")
print("sun","haoyu")
--------------------------------------------------
运行结果如下:
sunhaoyu
sun haoyu
可知,不添加逗号分隔符,字符串之间没有间隔
-----------------------------------------------------
print("www", "baidu", "com", sep=".") # 设置间隔符
----------------------------------------------------
运行结果如下:
www.snh48.com
file的引用:
?
?1.2.1格式化输出
老的格式化:占位符
%d: digit 整型
%f: float 浮点型
%s: string 字符串
data = 1
data2 = 2.2
data3 = "123"
print(data, data2, data3)
print("data is %d, data2 is %f, data3 is %s" % (data, data2, data3))
#限制显示的宽度: %numd %nums
data = 9999
print("data is %4d, data2 is %.1f, data3 is %6s" % (data, data2, data3))
data = 1
print("data is %4d, data2 is %.1f, data3 is %6s" % (data, data2, data3))
#左对齐:-
print("data is %-4d, data2 is %.1f, data3 is %-6s" % (data, data2, data3))
#右对齐:+
print("data is %+4d, data2 is %.1f, data3 is %+6s" % (data, data2, data3))
输出:
1 2.2 123
data is 1, data2 is 2.200000, data3 is 123
data is 9999, data2 is 2.2, data3 is 123
data is 1, data2 is 2.2, data3 is 123
data is 1 , data2 is 2.2, data3 is 123
data is +1, data2 is 2.2, data3 is 123
Process finished with exit code 0
新的格式化:format
f/F"{variable_name}"
{variable_name:宽度}
data = 1
data2 = 2.2
data3 = "123"
print(f"data is {data}, data2 is {data2}, data3 is {data3}")
print(F"data is {data:4}, data2 is {data2}, data3 is {data3}")
data = 9999
print(f"data is {data:4}, data2 is {data2}, data3 is {data3}")
#左对齐: <
data = 1
print(f"data is {data:<4}, data2 is {data2}, data3 is {data3}")
#右对齐: >
print(f"data is {data:>4}, data2 is {data2}, data3 is {data3}")
#居中对齐: ^
print(f"data is {data:^4}, data2 is {data2}, data3 is {data3}")
# 填充: *
print(f"data is {data:*^4}, data2 is {data2}, data3 is {data3}")
输出:
1 2.2 123
data is 1, data2 is 2.2, data3 is 123
data is 1, data2 is 2.2, data3 is 123
data is 9999, data2 is 2.2, data3 is 123
data is 1 , data2 is 2.2, data3 is 123
data is 1, data2 is 2.2, data3 is 123
data is 1 , data2 is 2.2, data3 is 123
data is *1**, data2 is 2.2, data3 is 123
Process finished with exit code 0
|