Blogger Information
Blog 29
fans 0
comment 0
visits 18425
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
python占位符、格式化输出、转义字符
CC
Original
2046 people have browsed it

1.常用的占位符
占位符,顾名思义就是插在输出里占位的符号。
%d:整数型(int)占位符
%s:字符串型(str)占位符
%f:浮点型(float)占位符
(%.nf:自主保留n位小数)

  1. age=14
  2. #,会空一个格子
  3. print("Youare",age,"yearsold")
  4. print("youare%dyearsold"%age)
  1. age=14
  2. weight=45.56
  3. print("ni" ,age,"sui",",","tizhong" ,weight, "kg")
  4. print("ni%dsui,tizhong%skg"%(age ,weight))
  1. name="幽监伊梦"
  2. age = 18.5
  3. print("我的名字是%s,我的年龄是%d"%( name ,age))
  4. print("输出小数: %f" %age)

2.format
format是官方推荐使用的方式,基本语法是通过{}和:来代替以前的%,format使用十分灵活,功能强大,不需要考虑数据类型。

  1. print("我要打印{}和{}" . format("hel1o" ,"world"))
  1. print("ID: {name}, 年龄: {age}" . format(name= "幽蓝伊梦" ,age=18))

3.转义字符
换行字符\n

  1. print("第一行\n第二行")

pyton默认换行不换行可以使用end

  1. print("123456" ,end="")
  2. print("123456" )
  3. print('---')
  4. print("123456" )
  5. print("123456" )

4.main使用

  1. def main(a):
  2. print("hello",a)
  3. # 原本写法
  4. main(2)
  5. if __name__=="__main__":
  6. # 当程序执行时,调用函数
  7. main(1)
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post