1.常用的占位符
占位符,顾名思义就是插在输出里占位的符号。
%d:整数型(int)占位符
%s:字符串型(str)占位符
%f:浮点型(float)占位符
(%.nf:自主保留n位小数)
age=14#,会空一个格子print("Youare",age,"yearsold")print("youare%dyearsold"%age)
age=14weight=45.56print("ni" ,age,"sui",",","tizhong" ,weight, "kg")print("ni%dsui,tizhong%skg"%(age ,weight))
name="幽监伊梦"age = 18.5print("我的名字是%s,我的年龄是%d"%( name ,age))print("输出小数: %f" %age)
2.format
format是官方推荐使用的方式,基本语法是通过{}和:来代替以前的%,format使用十分灵活,功能强大,不需要考虑数据类型。
print("我要打印{}和{}" . format("hel1o" ,"world"))
print("ID: {name}, 年龄: {age}" . format(name= "幽蓝伊梦" ,age=18))
3.转义字符
换行字符\n
print("第一行\n第二行")
pyton默认换行不换行可以使用end
print("123456" ,end="")print("123456" )print('---')print("123456" )print("123456" )
4.main使用
def main(a):print("hello",a)# 原本写法main(2)if __name__=="__main__":# 当程序执行时,调用函数main(1)
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号