python实战训练---基础练习(20)

计算复读次数

题目 计算字符串中子串出现的次数。

程序分析 无。

s1='xuebixuebixuebixuebixuebixuebixuebixue'
s2='xuebi'
print(s1.count(s2))

磁盘写入

题目 从键盘输入一些字符,逐个把它们写到磁盘文件上,直到输入一个 # 为止。

程序分析 无。

if __name__ == '__main__':
    from sys import stdout
    filename = input('输入文件名:\n')
    fp = open(filename,"w")
    ch = input('输入字符串:\n')
    while ch != '#':
        fp.write(ch)
        stdout.write(ch)
        ch = input('')
    fp.close()

磁盘写入II

题目 从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件"test"中保存。

程序分析 无。

if __name__ == '__main__':
    fp = open('test.txt','w')
    string = input('please input a string:\n')
    string = string.upper()
    fp.write(string)
    fp = open('test.txt','r')
    print (fp.read())
    fp.close()

磁盘读写

题目 :
有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列), 输出到一个新文件C中。

程序分析 无。

if __name__ == '__main__':
    import string
    fp = open('test1.txt')
    a = fp.read()
    fp.close()
 
    fp = open('test2.txt')
    b = fp.read()
    fp.close()
 
    fp = open('test3.txt','w')
    l = list(a + b)
    l.sort()
    s = ''
    s = s.join(l)
    fp.write(s)
    fp.close()

列表转字典

题目 列表转换为字典。

程序分析 无。

i = ['a', 'b']
l = [1, 2]
print (dict(zip(i,l)))

猜你喜欢

转载自blog.csdn.net/xdc1812547560/article/details/107715681