zoukankan      html  css  js  c++  java
  • 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






    转载请附上原文链接。
  • 相关阅读:
    IntelliJ IDEA 14.03 java 中文文本处理中的编码格式设置
    应聘感悟
    STL string分析
    CUDA SDK VolumeRender 分析 (1)
    BSP
    CUDA SDK VolumeRender 分析 (3)
    CUDA SDK VolumeRender 分析 (2)
    Windows软件发布时遇到的一些问题
    Ten Commandments of Egoless Programming (转载)
    复习下光照知识
  • 原文地址:https://www.cnblogs.com/bugbreak/p/12425056.html
Copyright © 2011-2022 走看看