python输入与输出165

时间:2023-03-09 17:16:07
python输入与输出165
 s = 'Hello,Runoob'

 print(s)

 str(s)
print(s) print(repr(s)) print(1/7)
print(str(1/7))
print(repr(1/7)) print('repr()的用法')
x = 10*3.25
y = 200*200
s = 'x的值为:'+str(x)+',y的值为:'+str(y)
print(s) s = 'x的值为:'+repr(x)+',y的值为:'+repr(y)
print(s) print('repr()可以转义特殊字符')
hello = 'hello ,runoob\n'
print(hello)#hello ,runoob hellos = repr(hello)
print(hellos)#'hello ,runoob\n' ts = ('Google','Runoob')
repr((x,y,ts))
print(type(ts))
print(x,y,ts)#32.5 40000 ('Google', 'Runoob') print('输出平方根与立方根')
for x in range(1,11):
# print(x.rjust(2),x*x,x*x*x),,AttributeError: 'int' object has no attribute 'rjust''
# print(x.rjust(2),x*x,x*x*x)
print(str(x).rjust(2),str(x*x).rjust(5),str(x*x*x).rjust(10)) '方式二'
for x in range(1,11):
# print('{0}{1}{2}'.format(x, x ** 2, x ** 3))
#2d即两个空格
print('{0:2d}{1:5d}{2:6d}'.format(x,x**2,x**3)) print('zfill()即补足空格')
for x in range(1,11):
# print(x.rjust(2),x*x,x*x*x),,AttributeError: 'int' object has no attribute 'rjust''
# print(x.rjust(2),x*x,x*x*x)
print(str(x).zfill(2),str(x*x).zfill(5),str(x*x*x).zfill(10)) 'format()的用法'
print('{0}和{1}'.format('Google','Runoob'))
print('{1}和{0}'.format('Google','Runoob'))
print('{name}网址:{site}'.format(name = '菜鸟教程',site = 'www.site.com'))
#点列表 Google, Runoob, 和 Taobao。
print('站点列表{0},{1},{other}'.format('Google','Runoob',other = 'Taobao')) '!a,!s,!r格式化某个值之前对其进行转换'
import math
#常量 PI 的值近似为: 3.141592653589793。
print('常量PI的近似值为{}'.format(math.pi))
print(type(math.pi)) #使用!r或者!s后输出的值都是float类型的?
pi2=math.pi
print('常量PI的近似值为:{!r}'.format(pi2))
print(type(pi2)) pi3=math.pi
print('常量PI的近似值为:{!s}'.format(pi3))
print(type(pi3)) #常量 PI 的值近似为 3.142。
print('常量PI的近似值为{0:.3f}'.format(math.pi))
print('常量PI的近似值为{0:.2f},{1:.3f}'.format(math.pi,math.pi))#常量PI的近似值为3.14,3.142 # Runoob ==> 2
# Taobao ==> 3
# Google ==> 1
#int类型的直接为:10d,如果是str类型的为:5,没有d
table = {'Runoob':2,'Taobao':3,'Google':1}
print(type(table))
for name,num in table.items():
print('{0:5}===>{1:10d}'.format(name,num))
print(type(num))
print(type(name)) #Runoob: 2; Google: 1; Taobao: 3(不会这是什么?)==============================================================================
table = {'Google': 1, 'Runoob': 2, 'Taobao': 3}
print('Runoob: {0[Runoob]:d}; Google: {0[Google]:d}; Taobao: {0[Taobao]:d}'.format(table)) #其中的5是什么意思呢?
import math
print('常量 PI 的值近似为:%5.3f。' % math.pi) 'input()从标准输入读入一行文本,这里的标准输入就是指的键盘'
# 请输入:菜鸟教程
# 你输入的内容是: 菜鸟教程
# str = input('请输入:')
# print('您输入的内容是:',str) f = open('E:\\foo.txt','a')
f.write('Python是一个非常好的语言。\n是的,的确非常好wb+,a!!')
f.close() print('写入并读取打印出来')
#这部分是写入文件中内容
f = open('E:\\foo.txt','w')
f.write('Python是一个非常好的语言。\n是的,的确非常好w!!')
#这部分是打开并读取文件内容
f = open('E:\\foo.txt','r')#没有这句是打印不出来内容的
str = f.read()
print(str) print('readline()的用法')
print('到这里了')
f = open('E:\\foo.txt','r')
str1 = f.readline()
print(str1)
f.close() print('readlines()的用法')
f = open('E:\\foo.txt','r')
str2 = f.readlines()
print(str2)
f.close() print('迭代读取每行')
f = open('E:\\foo.txt','r')
for line in f:
print(line,end='') print('读取写入的字数')
f = open('E:\\foo.txt','w')
num = f.write('Python是一个非常好的语言。\n是的,的确非常好w!!')
print(num) print("向 foo1.txt 文件中写入‘'www.runoob.com', 14’")
f = open('E\\foo1.txt','w')
value = 'www.runoob.com'
s = str(value)
f.write(s,14) print("向 foo1.txt 文件中写入‘'www.runoob.com', 14’")
f2 = open('E:\\foo1.txt','w+')
value = 'www.runoob.com'
s = str(value)
f2.write(s) f2 = open('E:\\foo1.txt','r')
s = f2.read()
# s = str(f2)
print(s)