zoukankan      html  css  js  c++  java
  • python学习笔记-day02 格式化输出

    格式化输出

    %s %d   其实%的作用就是占位

    另外一种也可以使用forma 函数;

    name=input("please input your name:")
    age=input("please input your age:")
    print("我的名字是{},我的年龄是{}".format(name,age))
    print("我的名字是%s,我的年龄是%s" %(name,age))
    View Code

    如果想按照一定的格式输出,可以使用% 占位符格式化输出:

    name='xuanxuan'
    msg='我的名字是 %s' % name
    print(msg)
    name=input("please input your name:")
    age=input("please input your age:")
    height=input("please inpur your height:")
    hobbi=input("please input your hobbi:")
    
    msg='''--------------information of %s----------------
          name: %s
          age:  %d
          height: %d
          hobbi: %s
          ----------------------end-----------------------
          ''' %(name,name,int(age),int(height),hobbi)
          
    print(msg)
          
    View Code

    如果你想在格式化输出的地方输出一个百分号比如:

    name='xuanxuan'
    age='12'
    height=163
    msg='name:%s,age:%s,height:%d,我是一个百分号5%' %(name,age,height)
    print(msg)
    View Code

    其实是会报错的:

    解决办法:

    在想要输出%的前面再加一个%才能输出想要的%:

    name='xuanxuan'
    age='12'
    height=163
    msg='name:%s,age:%s,height:%d,我就是一个百分号5%%' %(name,age,height)
    print(msg)
    View Code

    while... else

    当while循环被break打断时,不会执行后面的else部分;当while循环正常执行完之后才会执行else部分;

    这是while循环中当count累加到某个值,会跳出循环体,不会执行else

    count=1
    while True:
        print(count)
        count+=1
        if count>10:
            break
    else:
        print("我不会被执行的,因为while循环体中当count累加到10之后就会跳出循环体")
    View Code

    下面是while循环体正常运行,else部分会被执行的情况:

    count=1
    while count<10:
        print(count)
        count+=1
    else:
        print("while循环正常结束了,我是会被执行的哦~")
    View Code
    talk is cheap,show me the code
  • 相关阅读:
    操作系统基础知识与常见问题记录
    String
    Function
    HelloWorld
    替换空格
    二维数组中的查找
    建造者模式(Builder Pattern)
    单例模式(Singleton Pattern)
    工厂方法(Factory Pattern)
    设计模式概述
  • 原文地址:https://www.cnblogs.com/xuanxuanlove/p/9457746.html
Copyright © 2011-2022 走看看