python基础—day1 输入与输出
读取键盘输入
Python 提供了 input() 内置函数从标准输入读入一行文本,默认的标准输入是键盘。
实例
#!/usr/bin/python3
str = input("请输入:");
print ("你输入的内容是: ", str)
str = input("请输入:");
print ("你输入的内容是: ", str)
.print,输出函数
●print()函数可以输出哪些内容?
(1)print()函数输出的内容可以是数字
Print(520)
(2)print)函数输出的内容可以是字符串(char)
Print(‘hello’)
(3)print)函数输出的内容可以是含有运算符的表达式
●print)函数可以将内容输出的目的地
(1)显示器
(2)文件(注意点,1.所指定的盘存在。2.使用file= fp)
fp=open(‘D:/text.txt’,’a+’) #a+,文件存在就后面追加,不存在创建
Print(‘hello’,file=fp)
Fp.close() #--退出
●print()函数的输出形式
(1)换行
(2)不换行
Print(‘hello’,’wold’,’python’) #,分隔,在一行输出
str.format() 的基本使用如下:
print('{}网址: "{}!"'.format('百度', 'www.baidu.com'))
#括号及其里面的字符 (称作格式化字段) 将会被 format() 中的参数替换。
#在括号中的数字用于指向传入对象在 format() 中的位置,如下所示
print('{0} 和 {1}'.format('Google', 'Runoob'))
#Google 和 Runoob
print('{1} 和 {0}'.format('Google', 'Runoob'))
#如果在 format() 中使用了关键字参数, 那么它们的值会指向使用该名字的参数。
print('{name}网址: {site}'.format(name='百度', site='www.baidu.com'))
#位置及关键字参数可以任意的结合:
print('站点列表 {0}, {1}, 和 {other}。'.format('Google', ‘Runoob’,othor=’Taobao’))
#!a (使用 ascii()), !s (使用 str()) 和 !r (使用 repr()) 可以用于在格式化某个值之前对其进行转化:
import math
print('常量 PI 的值近似为: {}。'.format(math.pi))
print('常量 PI 的值近似为: {!r}。'.format(math.pi))
#可选项 : 和格式标识符可以跟着字段名。 这就允许对值进行更好的格式化。 下面的例子将 Pi 保留到小数点后三位:
import math
print('常量 PI 的值近似为 {0:.3f}。'.format(math.pi))

浙公网安备 33010602011771号