美文网首页
Python中的输出

Python中的输出

作者: 晃里晃荡 | 来源:发表于2018-03-30 23:10 被阅读4次

    第一种输出方式:print输出

    >>>print('Hello world!')
    Hello world!

    #通过转义字符\n进行换行输出
    >>>print('Hello\nworld!')
    Hello
    world!

    print 在输出后默认是换行的,如果要实现不换行输出,只需要在变量末尾加上 end="":

    # 不换行输出
    print(" 输出结果:", end=" " )
    print( a, end=" " )
    print(b, end=" " )
    输出结果:a b

    第二种输出方式:格式化输出

    >>>enscore = 100
    >>>chscore = 99
    >>>print("My English score is %d"%enscore)
    My English score is 100
    >>>print("My English score is %d....My Chinese score is %d"%(enscore,chscore))
    My English score is 100....My Chinese score is 99

    补充:

    要有输出,就需要先要有输入,上面的两种输入都是我们预先既定好的,但是如果想要程序获取我们人为输入的,那就需要用到等待输入。

    python3的用法:

    >>>input("请输入密码:")
    请输入密码:▎

    python2的用法:

    >>>raw_input("请输入密码:")
    请输入密码:▎
    >>>input("请输入密码:")
    请输入密码:▎

    说明:

    1. 在Python3中,只能用input(),不能用raw_input()。
    2. 在Python2中,input()和raw_input()都可以用,但两者区别在于,input()会把你输入的当作是表达式来看待,而raw_input()会把你输入的原封不动地保存下来。

    相关文章

      网友评论

          本文标题:Python中的输出

          本文链接:https://www.haomeiwen.com/subject/udjwcftx.html