• python3------print 打印输出


    # 1、打印字符串
    print('hello world!')

    输出结果:

    hello world!

    #2、打印中文字符串
    print ('世界,你好!')

    输出结果:

     
    世界,你好!

    # 3、打印变量
    a=10
    print(a)

    输出结果:

     
    10

    # 4、打印自定义函数值
    def add2(x,y):
        return [x+2,y+2]
    print (add2(0,0))

    输出结果:

     
    [2, 2]

    # 5、python每执行一次print语句时,自动在打印内容末尾加入一个换行符
    print('hello world!',)
    print ('世界,你好!')

     输出结果:
    hello world!
    世界,你好!

    # 6、# 如下print后添加end='',相当于执行print语句时,在打印内容后加一个空格,默认end=' '换行
    print('hello world!',end=' ')
    print('世界,你好!')

    输出结果:

     
    hello world! 世界,你好!

    # 7、两个字符串拼接打印 用"+"
    print('hello world!'+'世界,你好!')

    输出结果:

     
    hello world!世界,你好!

    # 8、两个字符串换行打印 用空格加转义字符 print('hello world! 世界,你好!')

    输出结果:

     
    hello world! 
    世界,你好!

    #9、利用制表符 控制打印内容水平间隔,目的对齐, 8个字符的宽度为一块,每8个字符后面都有一个制表点
    print('number Square Cube')
    for i in range(1,11):
        print(i,' ',i**2,' ',i**3)

    输出结果

     
    number 	Square 	Cube
    1 	 1 	 1
    2 	 4 	 8
    3 	 9 	 27
    4 	 16 	 64
    5 	 25 	 125
    6 	 36 	 216
    7 	 49 	 343
    8 	 64 	 512
    9 	 81 	 729
    10 	 100 	 1000

    # 10、格式化字符串输出 %s字符串格式输出,%d整数格式输出 %f浮点型格式输出 %e科学计数法 %g自动浮点计数法
    print('I love %s.'%'marry')
    print('She is %d years old.' %18)
    print('Her height is %fm.' %1.63)
    print('Her height is %.2fm.' %1.634)

    输出结果:

     
    I love marry.
    She is 18 years old.
    Her height is 1.630000m.
    Her height is 1.63m.

    #11、format()方法形如
    print('She is {} years old.Her height is {}.'.format(18,1.63))

    输出结果:
    She is 18 years old.Her height is 1.63.
  • 相关阅读:
    centos6安装mono
    Flashcache系统管理员手册
    【ZT】超乎想象 HTML5九大超酷特效体验
    程序员不可不知的C#代码规范
    【推薦】帮你炼成软件架构师的97件事
    [ZT]智能客户端(Smart Client)
    【ZT】成就大型高性能网站的十项规则
    通過SQL取出所有周六/周日的日期到Table
    【杯具】面试才说一句话就被轰出来了
    Integer在webservice的传递
  • 原文地址:https://www.cnblogs.com/leaf9/p/8734661.html
Copyright © 2020-2023  润新知