4.26-python学习笔记(变量及命名、字符串、格式化字符串print,format,%...)
2024-08-21 14:43:44
参考书目:《Learn Python The Hard Way》
cars=100
print('there are ',cars,'cars available.')
##练习5 更多变量打印
my_name='zxx'
my_age=22
my_height=162
my_weight='secret'
my_eyes='black'
my_teeth='white'
my_hair='black'
print("let's talk about %s."%my_name)
print("she's %d inches tall."%my_height)
print("she's weight is %s."%my_weight)
print("actully that's not too heavy.")
print("she's got %s eyes and %s hair."%(my_eyes,my_hair))
print("her teeth are usually %s depending on the coffe."%my_teeth)
print("if i add %d ,%d, i get %d."%(my_age,my_height,my_age+my_height))
print('%.2f'%2.6345) #保留两位小数(%后加.)
#%c 转换成字符(ASCII 码值,或者长度为一的字符串)
#%r 优先用repr()函数进行字符串转换(Python2.0新增)
#%s 优先用str()函数进行字符串转换
#%d / %i 转成有符号十进制数
#%u 转成无符号十进制数(阿拉伯数字1,2,3,4.....10)
#%o 转成无符号八进制数(0,1,2,...7;逢八进一)
#%x / %X (Unsigned)转成无符号十六进制数(x / X 代表转换后的十六进制字符的大小写)
#%e / %E 转成科学计数法(e / E控制输出e / E)
#%f / %F 转成浮点数(小数部分自然截断)
#%g / %G %e和%f / %E和%F 的简写
#%% 输出%
##练习6 字符串和文本
x="there are %d types of people."%10
binary='binary'
do_not="don't"
y="those who kown %s and those who %s."%(binary,do_not)
print(x)
print(y)
print("i said :%r."%x)
#将%r改成$s,x字符串输出的时候没有单引号,即%r输出有单引号
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('%s%s'%(w,e))
##练习7 更多打印
print("marry had a little lamb.")
print("its fleece was white as %s."%'snow')
print("and everyone that mary went.")
print("."*10) #打印十个.
end1="C"
end2="h"
end3="e"
end4="e"
end5="s"
end6="e"
end7="B"
end8="u"
end9="r"
end10="g"
end11="e"
end12="r"
print (end1+end2+end3+end4+end5+end6,end=" ")
print(end7+end8+end9+end10+end11+end12) #在一行内打印,利用end参数end=""(无缝连接)end=" "(字符串间有空格)
##练习8 :打印,打印
formatter="%r %r %r %r"
print(formatter %(1,2,3,4))
print(formatter %("one","two","three","four")
print(formatter %("i had this thing.",
"that you could type up right.",
"but it didn't sing",
"so i said goodnight.")
)
#调试不成功的代码69-75
##练习9 打印,打印,打印
days="mon tue wed thu fri sat sun"
months="jan\nfeb\nmar\napr\nmay\njun\njul\naug" #\n换行标识符
print("here are the days:",days)
print("here are the months:",months)
print("""
there's something going on here.
with the three double-quotes.
we'll ba able to type as much as we like.
even 4 lines if we want,or 5,or 6.
""")
#"""三引号:打印多行字符串
最新文章
- Android开发学习—— 消息队列
- bootstrap在jsp中的应用
- android srudio使用HttpClient
- VMware克隆SUSE网卡配置
- solr多核配置
- lua if
- java窗口添加背景
- php反射机制获取未知类的详细信息
- Codeforces Gym 100431G Persistent Queue 可持久化队列
- Linux 命令 - file: 确定文件类型
- JsTree异步加载数据实现多级菜单
- 字符编码笔记:ASCII,Unicode和UTF-8,附带 Little endian和Big endian的解释
- WPF 打开文件 打开路径对话框
- 浅谈jquery关于select框的取值和赋值
- 网络基础知识HTTP(1) --转载
- 我用过的linux命令--安装Hadoop
- php error _report
- PHP 中使用socket
- Django_项目初始化
- ubuntu 修改系统时间无效
热门文章
- PreApplicationStartMethod特性说明
- Asp.Net MVC Identity 2.2.1 使用技巧(三)
- 寒假关于计算机课程的学习计划(第二次作业<;二>;)
- 项目管理利器-Maven(Windows安装)
- BZOJ 3211 花神游历各国 线段树平方开根
- ubuntu服务器下tomcat安装(不推荐使用apt-get)
- [HNOI2010]公交线路
- 【[SDOI2008]Sandy的卡片】
- Java50道经典习题-程序20 求前20项之和
- selenium断言的分类