明天的面试也不知道公司会出什么题,为了平静一下心情,做几个python解解闷,自己模拟一下。
1)从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件"e:/PythonAAA/A/test.txt"中保存。
string=str(input("请输出一句小写字母的字符串:"))
f=open("e:/PythonAAA/A/test.txt","a")
string=string.upper()
f.write(string)
print("文件的内容是"+string)
f.close()
2)有两个磁盘文件A和B,A放一行字母"a,e,i,o,u",B方一行字母"love,xtz",要求把这两个文件中的信息合并(按字母顺序排列), 输出到一个新文件C中。
f=open("e:/PythonAAA/A/A.txt","w+")
f.write("aeiou")
f.close() #A文件搞定内容
f=open("e:/PythonAAA/A/B.txt","w+")
f.write("love,xtz")
f.close() #B文件搞定内容
f=open("e:/PythonAAA/A/A.txt")
aaa=f.read()
f.close()
f=open("e:/PythonAAA/A/B.txt")
bbb=f.read()
f.close()
string=list(aaa+bbb)
string.sort()
s=‘‘
s=s.join(string)
s=s.strip(",") #把逗号去掉
f=open("e:/PythonAAA/A/C.txt","w+")
f.write(s)
print("A和B的文件内容已经有序的合并!")
f.close()3)计算字符串中子串出现的次数。比如,aaa=www.163.net,搜索net,显示1次。
aaa=str(input("请输入一个字符串:"))
bbb=str(input("请输入要查询的字段:"))
num=aaa.count(bbb)
num=str(num)
print("子串出现的次数是"+num)
4)读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。比如输入4,就返回****,输入6,返回******,以此类推。
n=1
while n<=7:
a=int(input("please Enter a num:"))
if a >= 10:
print("NOOOOOO~")
break
else:
print(a * "*")
n=n+1本文出自 “生活就是等待戈多” 博客,请务必保留此出处http://chenx1242.blog.51cto.com/10430133/1776704
原文地址:http://chenx1242.blog.51cto.com/10430133/1776704