一入Python深似海--print
Python -- PRINT
2023-09-11 14:21:00 时间
先给大家来个干货^~^,学习Python的一个好站点,http://learnpythonthehardway.org/book/
经典样例
以下是几个老经典的样例喽,刚接触Python的能够敲一敲,看看结果喽!
my_name='Zed A. Shaw' my_age=35#not a lie my_height=74#inches my_weight=180#1bs my_eyes='Blue' my_teeth='white' my_hair='Brown' print my_name print "Let's talk about %r" %my_name print "He's %d inches tall." %my_height print "He's %d pounds heavy." %my_weight print "Actually that's not too heavy." print "He's got %s eyes and %s hair."%(my_eyes,my_hair) print "His teeth are usually %s depending on the coffee."%my_teeth #this line is tricky,try to get it exactly right print "If I add %d, %d,and %r I get %d."%(my_age,my_height,my_weight,my_age+my_height+my_weight)
x="There are %d types of people."%10 binary="binary" do_not="don't" y="Those who know %s and those who %s"%(binary,do_not) print x print y print "I said: %r."%x print "I also said: '%s'."%y hilarious=False joke_evaluation="Isn't that joke so funny?! %r" print joke_evaluation % hilarious w="This is the left side of..." e="a string with a right side" print w+e
print "Mary had a little lamb." print "Its fleece was white as %s."%'snow' print "Its fleece was white as %r."%'snow' print "And everywhere that Mary went." print "."*10 #output . 10 times end1="c" end2="h" end3="e" end4="e" end5="s" end6="e" #watch that comma at the end. print end1+end2+end3, print end4+end5+end6
formatter="%r %r %r %r" print formatter %(1,2,3,4) print formatter %("one","two","three","four") print formatter %(True,False,False,True) print formatter %(formatter,formatter,formatter,formatter) print formatter %( "I had this thing.", "That you could type up right.", "But it didn't sing.", "So I said goodnight." )
days="Mon Tue Wed Thu Fri Sat Sun" months="Jan\nFeb\nMar\nApr\nMay\nJun\nAug" print "Here are the days:",days print "Here are the months:",months print "Here are the months: %r"%months #That's how %r formatting works; #it prints it the way you wrote it (or close to it). It's the "raw" format for debugging. print """ There's something going on here. whith the three double-quotes. we'll be able to type as much as we like. Even 4 lines if we want, or 5, or 6. """
tabby_cat="\tI'm stabbed in." persian_cat="I'm split\non a line." backslash_cat="I'm \\ a \\ cat." fat_fat=""" I'll do a list: \t* Cat food \t* Fishies \t* Catnip\n\t* Grass """ print tabby_cat print persian_cat print backslash_cat print fat_fat
%r与%s的差别
我的总结是这么个点: %r与%s的差别
That's how %r formatting works; it prints it the way you wrote it (or close to it). It's the "raw" format for debugging.
Always remember this: %r is
for debugging, %s is for displaying.
相关文章
- Python小游戏 -- 猜数字
- 编程语言理解1-c语言,c++,java和python这些语言有何区别?编译型编程语言编译语言,解释型编程语言解释型语言
- python删除、替换字符串某字符后的字符串(删除字符串、替换字符串、strip、split、rstrip、lstrip、replace)
- 【华为OD机试真题 python】斗地主之顺子【2022 Q4 | 200分】
- 学会Python怎么找兼职?
- 【python小脚本】,如何优雅加载Yaml配置文件
- Python树莓派编程2.7 总结
- python之字符串格式化(format)
- Python爬虫技术--基础篇--内建模块XML和HTMLParser
- Python爬虫技术--基础篇--多进程
- Python爬虫技术--基础篇--面向对象编程(中)
- Python 理解 精灵 和 精灵组
- Python 数据存储--shelve模块
- python中pyqt5的进度条--python实战(十)
- python实用基本之--golb.glob
- Fibonacci数Python的四种解法
- Python科学计算生态圈--Scipy
- 哪个更适合数据分析:R 还是 Python?
- Python学习--Selenium模块学习(2)
- 【Python注意事项】如何理解python中间generator functions和yield表情
- Python之Beautiful Soup 4使用实例
- 学习python第三天单行函数
- Python算术运算符
- 【Linux】【压测】基于python Locust库实现自动化压测实践
- python--七夕小游戏