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






    转载请附上原文链接。
  • 相关阅读:
    Lightoj 1422
    BZOJ 1801 [AHOI 2009] 中国象棋(DP)
    [SCOI2008]天平
    [SCOI2008]奖励关
    [USACO08JAN]haybale猜测Haybale Guessing
    [Sdoi2016]征途
    [SHOI2014]概率充电器
    [USACO08JAN]电话线Telephone Lines
    [HEOI2016]排序
    友好的生物
  • 原文地址:https://www.cnblogs.com/bugbreak/p/12425056.html
Copyright © 2011-2022 走看看