Python内置函数之 print - 屏幕输出(打印)内容
print()
注:此语法适用于Python 3.x
作用:
在屏幕输出(打印)内容
必要操作:
内置函数,无需import方式导入
帮助查看:
1 | >>> help ( print ) |
方法(函数):
## Python 2.x 和3.x 通用打印方法
1 | >>> print ( "要打印的内容。" ) |
## 定义变量
1 2 | >>> check_cpu_count = os.cpu_count() >>> queue_maxsize = 3 |
## 输出一个参数,方式1:
1 2 3 | >>> print ( "CPU有%d个线程" % check_cpu_count) CPU有 2 个线程 >>> |
## 输出一个参数,方式2:参考
1 2 3 4 5 | >>> import datetime >>> t = datetime.date.today() >>> print ( "today = " , t) today = 2022 - 12 - 02 >>> |
## 输出两个参数:
1 2 3 | >>> print ( "CPU有%d个线程,设置的线程queue_maxsize=%d" % (check_cpu_count,queue_maxsize)) CPU有 2 个线程,设置的线程queue_maxsize = 3 >>> |
## 转义变量,就是将{}里识别成变量,f是转义,r是不转义,原样输出
1 2 3 | >>> print (f "CPU有{check_cpu_count}个线程,设置的线程queue_maxsize={queue_maxsize}" ) CPU有 2 个线程,设置的线程queue_maxsize = 3 >>> |
## 识别os.cpu_count()函数。注意有些函数本身不需要()
1 2 3 | >>> print (f "CPU有{os.cpu_count()}个线程,设置的线程queue_maxsize={queue_maxsize}" ) CPU有 2 个线程,设置的线程queue_maxsize = 3 >>> |
## 识别 函数的变量
1 2 3 4 5 | >>> url = 'https://www.baidu.com' >>> r = requests.get(url) >>> print (f "访问状态码:{r.status_code}" ) 访问状态码: 200 >>> |
## format格式化输出
1 2 3 | name = 'A' robot_name = 'B' print ( '你好,{},我是{}' . format (name,robot_name)) |
##多行输出
1 2 3 4 5 6 7 | >>> print ( '''123 ... 456 ... 789''' ) 123 456 789 >>> |
## 可以使用\n(换行) \t(制表符)
1 2 3 4 5 6 7 8 | >>> print ( '''123 ... \n\t456 ... 7890''' ) 123 456 7890 >>> |
## 模板方式输出,假设要输出一段 html 代码。这种方式还适合用到 变量 里。参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | html_tmplate= '' ' <html> < head > <title>%s< /title > < /head > <body> <a>Test< /a > <a>%s< /a > <a>%s< /a > < /body > < /html > '' ' title= "t_test" a1= "AAA" a2= "BBB" print(html_tmplate % (title, a1, a2)) |
-
print还可以直接写输出内容
1 | print(html_tmplate % ( "t_test" , "AAA" , "BBB" )) |
-
注意, 变量对应 %s 先后顺序
## 运算,运算两行代码执行的间隔时间,t1和t2是手工输入,所以这里是是测试两行输入并执行完,间隔多长时间。
1 2 3 4 | >>> t1 = time.time() >>> t2 = time.time() >>> print (f "t1和t2两行代码输入间隔总耗时:{t2 -t1}" ) 总耗时: 5.801331996917725 |
## 集合输出 (来源:路飞-8天高强度训练营-老尚\8天高强度Python训练营day1-4天课件\day3\dict.py)
1 2 3 4 5 6 | dic = { "Alex" :[23, "CEO" ,66000], "黑姑娘" :[24, "行政" ,4000], "佩奇" : [26, "讲师" ,40000], } |
### 判断内容是否在集合里
1 | print( "佩奇" in dic) |
### 输出在集合里的内容
1 | print(dic[ "佩奇" ]) |
-
### print 颜色输出 https://www.cnblogs.com/wutou/p/17811899.html
----------------------------------------------- 分割线 -------------------------------------------------------
注:以下内容适用于python 2.x版本
## 变量
1 2 | >>> fileName = 'test.txt' >>> keys = "123" |
## 变量打印
1 2 | >>> m = '文件名:%(fileName)s,关键字: %(keys)s' ; >>> print m % locals (); |
## 直接打印
1 | >>> print '文件名:%(fileName)s,关键字: %(keys)s' % locals (); |
## 打印文件名字符串用%s
1 2 | >>> print "文件名:%s" % (fileName) >>> print "关键字:%s" % (keys) |
## 打印关键字数字用%d,默认任何字符都是字符串,数字需要用int()转换
1 | >>> print "关键字:%d" % int ((keys)) |
## 多行输出
1 2 3 4 5 6 7 | >>> print '''123 ... 456 ... 7890''' ; 123 456 7890 >>> |
## 可以使用\n(换行) \t(制表符)
1 2 3 4 5 6 7 | >>> print '''123 ... \n\t456 ... 789''' ; 123 456 789 |
参考:
https://www.cnblogs.com/heyue13/p/12454607.html
__EOF__
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!