• 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






    转载请附上原文链接。
  • 相关阅读:
    PyTorch深度学习:60分钟入门(Translation)
    强化学习入门·
    leetcode 697. Degree of an Array
    耶路撒冷圣城起源笔记
    仓储机器人路径规划笔记
    算术编码原理
    ★房贷计算器 APP
    Sublime
    CocoaPods
    Xcode 6 创建 Objective-C category
  • 原文地址:https://www.cnblogs.com/bugbreak/p/12425056.html
Copyright © 2020-2023  润新知