• python3基础:格式化输出


    直接简单的输出
    #简单输出一个字符串
    >>>print('hello python apple')
    hello python apple

    #简单输出多个字符串
    >>>print('hello','python', 'apple')

    hello python apple
    #简单输出拼接字符串

    >>>print('hello'+ ' python'+ ' apple')
    hello  python  apple

    格式化输出 --为了让输出更加美观
    主要是
    1.%用法 2.format用法
    1.
    %()
    %d 数字
    %s 字符串
    %f 浮点数
    print('''===我的个人资料===
    name:%s
    sex:%s
    age:%d
    height:%f
    '''%('我的家在东北',"boy",15,70.66))
    #总结  %s放任何数据   %d%f 放数字
    
    

    输出结果:

    ===我的个人资料===
    name:我的家在东北
    sex:boy
    age:15
    height:70.660000

    2.另一种方式 {}字符串.format()

    print('''===我的个人资料===
    name:{0}
    sex:{1}
    age:{2}
    height:{3}
    '''.format('我的家在东北',"boy",15,70.66))

    输出结果:

    ===我的个人资料===
    name:我的家在东北
    sex:boy
    age:15
    height:70.66
    注意的点 1.{}要比()输入的少; 2.{}索引里面的值从0开始; 3.{}里面的索引都要同时给或者同时不给

    练习一下
    # name、age sex address hobby salary work_year
    print('''===ta的个人信息===
    name:%s
    sex:%s
    age:%d
    address:%s
    hobby:%s
    salary:%0.2f
    work_year:%d
    '''%('ta',"boy",15,'我的家','play basketball',7000.55,3))
    
    #第二种方法
    print('''===ta的个人信息===
    name:{}
    sex:{}
    age:{}
    address:{}
    hobby:{}
    salary:{}
    work_year:{}
    '''.format('ta',"boy",15,'我的家','play basketball',7000.55,3))
    
    

    输出结果

    ===ta的个人信息===
    name:ta
    sex:boy
    age:15
    address:我的家
    hobby:play basketball
    salary:7000.55
    work_year:3
    
    ===ta的个人信息===
    name:ta
    sex:boy
    age:15
    address:我的家
    hobby:play basketball
    salary:7000.55
    work_year:3






    转载请附上原文链接。
  • 相关阅读:
    0019. Remove Nth Node From End of List (M)
    0018. 4Sum (M)
    0278. First Bad Version (E)
    0273. Integer to English Words (H)
    0017. Letter Combinations of a Phone Number (M)
    0016. 3Sum Closest (M)
    0015. 3Sum (M)
    软件测试常见面试题
    如何快速掌握DDT数据驱动测试?
    selenium--三种等待方式
  • 原文地址:https://www.cnblogs.com/bugbreak/p/12425056.html
Copyright © 2020-2023  润新知