python:标准输入输出函数的使用

python标准输入输出:sys.stdin和sys.stdout

1.sys.stdin.readline() 可读取输入的内容(包括换行符)

    >>> import sys
    >>> sys.stdin.readline()
    abcdef
    'abcdef\n'
    >>> sys.stdin.readlines()
    abcd
    efg
    higk
    ['abcd\n', 'efg\n', 'higk\n']
    >>> sys.stdin.read()
    abcd
    efg 
    'abcd\nefg\n'

2.sys.stdout.write() 可输出字符串,返回字符串长度
示例可以看出,打印结果不包含’\n’,且只能打印字符串

    >>> sys.stdout.write('123')
    1233
    >>> a = sys.stdout.write('123')
    123>>> 
    >>> sys.stdout.write(123)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: write() argument must be str, not int

3.练习题:写一个函数,实现print函数的功能

    import sys
    def myprint(*args,sep=' ',end='\n'):
        flag = False
        for i in args:
            if flag:
                sys.stdout.write(sep)
            sys.stdout.write(str(i))
            flag = True
        sys.stdout.write(end)
        
    myprint('hello world')
    myprint(5,6,7,8,sep='#')
    myprint(1,2,3,4,sep=',',end=' ')
    
    >>>hello world
    >>>5#6#7#8
    >>>1,2,3,4

4.另外,fileno()返回文件描述符,0,1,2分别对应了标准输入,标准输出和标准错误输出

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值