zoukankan      html  css  js  c++  java
  • python 格式化输出

      格式化输出的意义在于一个字符串中包含了多个变量,这个时候需要用到占位符,然后在后面补全变量.,是代码更加简洁

    1.%s (%d,%i)

    %s 代表的是占位一个要传递进来一个字符串类型的变量,当然数值类型的变量同样可以

    %d,%i代表的是占位一个要传递进来的是一个数值类型的变量

    name = '张三'
    age = 28
    msg = '''
    -------学生信息表------------
        名字:%s
        年龄:%d'''%(name,age)
    
    print(msg)
    

    <运行结果>

    -------学生信息表------------
        名字:张三
        年龄:28  

    2.f''名字:{},年龄:{}''

    name = '张三'
    age = 18
    msg = f'''
    -------学生信息表------------
            名字:{name}
            年龄:{age}'''
    print(msg)
    

    <运行结果>

    -------学生信息表------------
        名字:张三
        年龄:18

     3.format方法格式化输出

    第一种:元组下标法

    name = '张三'
    age = 20
    msg = """
        张三
        {0}
        {2}
        {1}
    """
    msg1 = msg.format('李四','王五','马六')
    print(msg1)
    

    <运行结果>

        张三
        李四
        马六
        王五
    

    第二种:字符串直接传递法

    name = '''
            张三
            李四
            {}
            {}
            '''
    name1 = name.format('王五','马六')
    print(name1)
    

    <运行结果>

            张三
            李四
            王五
            马六   
    

    第三种:变量赋值法

    name = '''
            张三
            李四
            {a}
            {b}
            '''
    name1 = name.format(a='王五',b='马六')
    print(name1)
    

    <运行结果>  

            张三
            李四
            王五
            马六
    

      

      

  • 相关阅读:
    External Interrupts in the x86 system. Part 1. Interrupt controller evolution
    虚拟机中断
    内核中断
    交换机三层转发vlanif
    centos 电池
    ironic port + host_id +device id
    arping
    2018-7-29-C#-强转会不会抛出异常
    2018-7-29-C#-强转会不会抛出异常
    2019-2-2-VisualStudio-扩展开发-添加菜单
  • 原文地址:https://www.cnblogs.com/duoduoyichen/p/10193706.html
Copyright © 2011-2022 走看看