python 笔记 更多的变量和字符串(string) ——12.22
2017-12-23 01:53
429 查看
习题 5: 更多的变量和打印
ex5.py
my_name = 'Zed A. Shaw'
my_age = 35
# not a lie
my_height = 74
# inches
my_weight = 180
# lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
print "Let's talk about %s."
%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 itexactly right
print "If I add %d, %d, and %d I get %d."
%(
my_age, my_height, my_weight, my_age
+ my_height + my_weight)
总结:
字符串格式化
自我测试:
ex5.1.py
tt =13.555
my_weight = 170
# lib
pounds = 0.453
*my_weight
print "you have %6.2f apples"
%tt
print "you have %3.2f apples"
%tt
print "you have %2.1f apples"
%tt
print "you have %1.1f apples"
%tt
print "you are %d pounds or %3.3f kg heavy."
%(my_weight,pounds)
习题 6: 字符串(string) 和文本
ex6.py
x ="There are %d types ofpeople."
% 10
binary = "binary"
do_not = "don't"
y ="Those who know %s andthose 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 rightside."
print w +e
运行结果:
Ex6_1.py (个人注释理解)
#-*- coding:utf-8 -*-
x = "Thereare %d types of people."
% 10
#将右边双引号的赋值给x,将10格式化输出(有符号整数)
binary = "binary"
#将binary赋值给binary
do_not = "don't"
#将don't
赋值给 do_not
y = "Thosewho know %s and those who %s."
% (binary,do_not)
#将右边双引号的赋值给y,分别将binary,do_not格式化输出(字符串)
print x
print y
print "Isaid: %r."
%x #此处前面好理解,但是原x的双引号成了单引号我需要再查查原因。
print "I also said: '%s'."
% y
hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"
printjoke_evaluation
% hilarious #两者对比还是有区别的。
print joke_evaluation,hilarious
w = "Thisis the left side of ..." #此处后者存在一个空格
e =" a string with a rightside."
print w +e
print w ,e
ex5.py
my_name = 'Zed A. Shaw'
my_age = 35
# not a lie
my_height = 74
# inches
my_weight = 180
# lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
print "Let's talk about %s."
%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 itexactly right
print "If I add %d, %d, and %d I get %d."
%(
my_age, my_height, my_weight, my_age
+ my_height + my_weight)
总结:
字符串格式化
格式 | 描述 |
%% | 百分号标记 |
%c | 字符及其ASCII码 |
%s | 字符串 |
%d | 有符号整数(十进制) |
%u | 无符号整数(十进制) |
%o | 无符号整数(八进制) |
%x | 无符号整数(十六进制) |
%X | 无符号整数(十六进制大写字符) |
%e | 浮点数字(科学计数法) |
%E | 浮点数字(科学计数法,用E代替e) |
%f | 浮点数字(用小数点符号) |
%g | 浮点数字(根据值的大小采用%e或%f) |
%G | 浮点数字(类似于%g) |
%p | 指针(用十六进制打印值的内存地址) |
%n | 存储输出字符的数量放进参数列表的下一个变量中 |
%r | 字符串 (采用repr()的显示) |
自我测试:
ex5.1.py
tt =13.555
my_weight = 170
# lib
pounds = 0.453
*my_weight
print "you have %6.2f apples"
%tt
print "you have %3.2f apples"
%tt
print "you have %2.1f apples"
%tt
print "you have %1.1f apples"
%tt
print "you are %d pounds or %3.3f kg heavy."
%(my_weight,pounds)
习题 6: 字符串(string) 和文本
ex6.py
x ="There are %d types ofpeople."
% 10
binary = "binary"
do_not = "don't"
y ="Those who know %s andthose 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 rightside."
print w +e
运行结果:
Ex6_1.py (个人注释理解)
#-*- coding:utf-8 -*-
x = "Thereare %d types of people."
% 10
#将右边双引号的赋值给x,将10格式化输出(有符号整数)
binary = "binary"
#将binary赋值给binary
do_not = "don't"
#将don't
赋值给 do_not
y = "Thosewho know %s and those who %s."
% (binary,do_not)
#将右边双引号的赋值给y,分别将binary,do_not格式化输出(字符串)
print x
print y
print "Isaid: %r."
%x #此处前面好理解,但是原x的双引号成了单引号我需要再查查原因。
print "I also said: '%s'."
% y
hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"
printjoke_evaluation
% hilarious #两者对比还是有区别的。
print joke_evaluation,hilarious
w = "Thisis the left side of ..." #此处后者存在一个空格
e =" a string with a rightside."
print w +e
print w ,e
相关文章推荐
- Python字符串String和变量 Variable
- [python 笔记2]列表、字符串、字典(list?string?map?)
- Python笔记(四)--变量类型之字符串
- Python学习笔记(一)--Python基础知识:变量、数据类型、模块与字符串
- python学习笔记2—python文件类型、变量、数值、字符串、元组、列表、字典
- Python笔记 - 变量和字符串
- (原创)c#学习笔记05--变量的更多内容03--字符串的处理
- Python 入门笔记 字符串 变量 标示符
- Python学习笔记:02 基础:变量、整数、字符串、赋值、基本输出、注释、代码块
- 零基础 入门学Python-学习笔记第二讲(变量与字符串)
- python3.4学习笔记(十五) 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python学习笔记--字符串操作string
- 【零基础入门学习Python笔记003】插曲之变量和字符串|课习题答案
- python字符串和变量相加时出现,coercing to unicode need string or buffer ?? found 错误的解决
- python字符串string笔记
- python字符串和变量相加时出现,coercing to unicode need string or buffer nonetype found python 错误的解决
- 三、python笔记之变量和字符串
- 编程小白的第一本 python 入门书 学习笔记02 第三章 变量与字符串
- 小甲鱼python视频003小插曲之变量和字符串--笔记 2017.12.29
- Python 核心编程笔记_Chapter_2 Note_3_赋值与变量_字符串_列表list_元组tuple_字典dict