- 浏览: 508432 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (310)
- PHP (46)
- Java (50)
- Perl (0)
- Bash (5)
- C# (9)
- JavaScript (5)
- InDesign (7)
- 百宝箱 (5)
- 排错 (3)
- 招聘 (5)
- PHP他山之石 (3)
- SQL(DML) (10)
- Flex (15)
- LAMP (1)
- DIV+CSS (1)
- C#2008 (4)
- LOGO (7)
- WorkLog (1)
- 工作 (26)
- Groovy (7)
- 海量数据 (4)
- C/C++ (1)
- Android (1)
- PYTHON (13)
- Java开发实战1200例 (1)
- Java枚举与泛型 (16)
- Java基础应用 (24)
- poj (3)
- Java数组与集合 (18)
- 疑难解析 (3)
- JavaWeb (8)
- Jython (2)
- 成功之路 (0)
- Golang (2)
- Spring (2)
- 微信小程序 (0)
最新评论
-
DawnBells:
...
java.util.concurrent 之六:使用Future类和Callable类 -
kanglecjr:
http://tieba.baidu.com/f?kz=101 ...
泰语字母好看的手写体 -
zxjlwt:
学习了。http://surenpi.com
java.util.concurrent 之六:使用Future类和Callable类 -
spring_springdata:
java jsoup开源框架demo使用实例教程源代码下载:h ...
JSOUP获取网页数据返回403错误(403 error loading URL,connection类) -
narochids:
MARK!
JavaScript+Ajax实例大全(1521例以上),可以随编辑随执行
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. #3.1基本字符串操作 >>> website = 'http://www.python.org' >>> website[-3:]='com' Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> website[-3:]='com' TypeError: 'str' object does not support item assignment #3.2 字符串格式化:精简版 >>> format = "Hello, %s. %s is enough for ya?" >>> values = ('world', 'Hot') >>> print format % values Hello, world. Hot is enough for ya? >>> format = "Pi with three decimals: %.3f" >>> from math import pi >>> print format % pi Pi with three decimals: 3.142 #模板字符串 >>> from string import Template >>> s = Template('$x, glorious $x!') >>> s.substitute(x='slurm') 'slurm, glorious slurm!' >>> s = Template("It's ${x}tastic!") >>> s.substitute(x='slurm') "It's slurmtastic!" >>> s = Template("Make $$ selling $x!") >>> s.substitute(x='slurm') 'Make $ selling slurm!' >>> s = Template('A $thing must never $action.') >>> d={} >>> d['thing']='gentleman' >>> d['action']='show his socks' >>> s.substitute(d) 'A gentleman must never show his socks.' >>> #saft_substitute不会因缺少值或不正确使用$字符而出错. >>> '%s plus %s equlas %s' % (1,1,2) '1 plus 1 equlas 2' >>> '%s plus %s equlas %s' % 1,1,2 Traceback (most recent call last): File "<pyshell#24>", line 1, in <module> '%s plus %s equlas %s' % 1,1,2 TypeError: not enough arguments for format string >>> '%s plus %s equlas %s' % 1,1,2 # Lacks parentheses! Traceback (most recent call last): File "<pyshell#25>", line 1, in <module> '%s plus %s equlas %s' % 1,1,2 # Lacks parentheses! TypeError: not enough arguments for format string #3.3 字符串格式化:完整版 #3.3.1简单转换 >>> 'Price of eggs: $%d' % 42 'Price of eggs: $42' >>> 'Hexadecimal price of eggs: %x' % 42 'Hexadecimal price of eggs: 2a' >>> from math import pi >>> 'Pi: %f...' % pi 'Pi: 3.141593...' >>> 'Very inexact estimate of pi: %i' % pi 'Very inexact estimate of pi: 3' >>> 'Using str: %s' % 42L 'Using str: 42' >>> 'Using repr: %r' % 42L 'Using repr: 42L' #3.3.2字段宽度和精度 >>> '%10f' % pi ' 3.141593' >>> '%10.2f' % pi ' 3.14' >>> '%.2f' % pi '3.14' >>> '%.5s' % 'Guido van Rossum' 'Guido' >>> '%.*s' % (5, 'Guido van Rossum') 'Guido' #3.3.3符号, 对齐和0填充 >>> '%010.2f' % pi '0000003.14' >>> 010 8 >>> '%-10.2f' % pi '3.14 ' >>> print ('% 5d' % 10) + '\n' + ('% 5d' % -10)F SyntaxError: invalid syntax >>> print ('% 5d' % 10) + '\n' + ('% 5d' % -10) 10 -10 >>> print ('% 5d' % 10) + '\n' + ('% 5d' % -10)F SyntaxError: invalid syntax >>> print ('% 5d' % 10) + '\n' + ('% 5d' % -10) 10 -10 >>> print ('%+5d' % 10) + '\n' + ('%+5d' % -10) +10 -10 #代码清单3-1 字符串格式化示例 #3.4字符串方法 #string模块还包括一些不能作为字符串方式使用的常量和函数 >>> import string >>> string.digits '0123456789' >>> string.letters 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' >>> string.lowercase 'abcdefghijklmnopqrstuvwxyz' >>> string.uppercase 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.punctuation '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' >>> string.printable '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c' >>> string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' #3.4.1 find >>> 'With a moo-moo here. and a moo-moo there'.find('moo') 7 >>> title = "Monty Python's Flying Circus" >>> title.find('Monty') 0 >>> title.find('Python') 6 >>> titie.find('Flying') Traceback (most recent call last): File "<pyshell#59>", line 1, in <module> titie.find('Flying') NameError: name 'titie' is not defined >>> title.find('Flying') 15 >>> title.find('Zirquss') -1 >>> subject = '$$$ Get rich now!!! $$$' >>> subject.find('$$$') 0 >>> subject.find('$$$', 1) 20 >>> subject.find('!!!') 16 >>> subject.find('!!!', 0, 16) -1 #3.4.2 join >>> seq = [1,2,3,4,5] >>> sep = '+' >>> sep.join(seq) Traceback (most recent call last): File "<pyshell#69>", line 1, in <module> sep.join(seq) TypeError: sequence item 0: expected string, int found >>> seq = ['1','2','3','4','5'] >>> sep.join(seq) '1+2+3+4+5' >>> dirs = '','usr','bin','env' >>> '/'.join(dirs) '/usr/bin/env' >>> print 'C:' + '\\'.join(dirs) C:\usr\bin\env #3.4.3 lower >>> 'Trondheim Hammer Dance'.lower() 'trondheim hammer dance' >>> if 'Gumby' in ['gumby','smith','jones']: print 'Found it!' >>> name='Gumby' >>> names=['gumby','smith','jones'] >>> if name.lower() in names: print 'Found it!' Found it! #标题转换 >>> "that's all folks".title() "That'S All Folks" >>> import string >>> string.capwords("that's all, folks") "That's All, Folks" #3.4.4 replace >>> 'This is a test'.replace('is','eez') 'Theez eez a test' #3.4.5 split >>> '1+2+3+4+5'.split('+') ['1', '2', '3', '4', '5'] >>> '/usr/bin/env'.split('/') ['', 'usr', 'bin', 'env'] >>> 'Using the default'.split() ['Using', 'the', 'default'] #3.4.6 strip 相当于Java 中的 String.trim() >>> ' internal whitespace is kept '.strip() 'internal whitespace is kept' >>> names = ['gumby','smith','jones'] >>> name = 'gumby' >>> if name in names: print 'Found it!' Found it! >>> '*** SPAM * for * everyone!!! ***'.strip(' *!') 'SPAM * for * everyone' #3.4.7 translate >>> from string import maketrans >>> table = maketrans('cs', 'kz') >>> len(table) 256 >>> table[97:123] 'abkdefghijklmnopqrztuvwxyz' >>> maketrans('','')[97:123] 'abcdefghijklmnopqrstuvwxyz' >>> table = maketrans('cs','kz') >>> 'this is an incredible test'.translate(table) 'thiz iz an inkredible tezt' >>> 'this is an incredible test'.translate(table,' ') 'thizizaninkredibletezt' #非英语字符串问题 table = maketrans('X','x') word = 'Xxx' print word.translate(table).lower() print u'Xxx'.lower() #小结 #本章介绍了字符串的两种非常重要的使用方式 #字符串格式化: 求模操作符(%)可以用来将其他值转换为包含转换标志的字符串,例如%s. 它还能用来对值进行不同方式的格式化, #包括左右对齐, 设定字段宽度以及精度,增加符号(正负号)或者左填充数字0等. #字符串方法 有些非常有用,比如split和join,有些则用得很少,比如istitle或capitalize. #本章的新函数 #string.capwords(s[, sep]) 使用split函数分割字符串s(以sep为分隔符),使用capitalize函数将分割得到的各单词首字母大写,并且使用join函数以sep为分隔符 #将各单词连接起来 #string.maketrans(from,to) 创建用于转换的转换表 #接下来学什么 列表, 字符串和字典是Python中最重要的3种数据类型.
代码清单3-1 字符串格式化示例
#e3-1 #使用给定的宽度打印格式化后的价格列表 width = input('Plese enter width: ') price_width = 10 item_width = width - price_width; #减号(-1)用来左对齐数值 header_format = '%-*s%*s' format = '%-*s%*.2f' print '=' * width print header_format % (item_width, 'Item', price_width, 'Price') print '-' * width print format % (item_width, 'Apples', price_width, 0.4) print format % (item_width, 'Pears', price_width, 0.5) print format % (item_width, 'Cantaloupes', price_width, 1.92) print format % (item_width, 'Dried Apricots (16 oz.)', price_width, 8) print format % (item_width, 'Prunes (4 lbs.)', price_width, 12) print '=' * width #python e3-1.py #Plese enter width: 35 #=================================== #Item Price #----------------------------------- #Apples 0.40 #Pears 0.50 #Cantaloupes 1.92 #Dried Apricots (16 oz.) 8.00 #Prunes (4 lbs.) 12.00 #===================================
发表评论
-
一键执行脚本即可把用户名和密码更新到指定路径下相应的配置文件(支持迭代)
2014-12-14 21:46 8391) 数据库和相关应用的用户名, 密码等敏感信息加密之后集 ... -
Python3.x 中,chr() 和 ord() 默认支持unicode
2014-09-14 15:44 12451In Python 2.x D:\Python27> ... -
如何让python的print函数不自动换行
2014-09-14 15:12 3973使用Python语言的print函数时,输出的每一行都会自 ... -
Python Bible - Ch23 多线程编程
2014-09-10 06:08 979''' 在Python中,可以通过继承threading ... -
Python基础教程之第9章 魔法方法, 属性和迭代器
2014-06-09 06:11 2844#魔法方法, 属性 和 迭代器 D:\>pytho ... -
Python基础教程之第8章 异常
2014-06-03 06:06 6229#Chapter 8 异常 Python 2.7.5 ( ... -
Python基础教程之第7章 更加抽象
2014-06-02 18:43 1154D:\>python Python 2.7.5 ( ... -
Python基础教程之第6章 抽象
2014-05-29 19:27 2369Python 2.7.5 (default, May 15 ... -
Python基础教程之第5章 条件, 循环和其他语句
2014-05-29 09:57 1326Python 2.7.5 (default, May 15 ... -
Python基础教程之第4章 字典: 当索引不好用时
2014-05-28 15:07 5928Python 2.7.5 (default, May 15 ... -
Python基础教程之第2章 列表和元组
2014-05-25 10:06 8203D:\>python Python 2.7.5 ( ... -
Python基础教程之第1章 基础知识
2014-05-22 05:45 1402#1.1 安装Python #1.1.1 Windows ...
相关推荐
python第3章答案_【Python基础教程】第3章字符串 所有标准的序列操作(索引,分⽚,乘法,判断成员资格,求长度,最⼤值,最⼩值)对字符串都是同样适⽤的。 3.1 字符串是不可变的 在Python中,字符串和元组⼀样,都是...
目录 第1 章 快速上手:基础知识 1.1 交互式解释器 1.2 算法是什么 1.3 数和表达式 ...1.9.1 从命令提示符运行 Python 脚本 ...1.10 字符串 ...1.10.1 单引号字符串以及对引号转义 ...第3 章 使用字符串
python教程答案第三章_Python基础教程(第三章) 字符串格式化: format = "hello %s,%s enough for ya" // format称为格式化字符串 value = ('world', 'hot') print format % value 模板字符串: from string ...
第三章 使用字符串 Pytho n 问题 内容回顾 运算符与表达式(算术、关系、逻辑) 变量与常量 序列:列表、字符串、元组... 通用序列操作 方法、函数的使用差异 – 方法:对象独有的,一般限于某类变量 (append,...
第3章 使用字符串 第4章 字典:当索引不好用时 第5章 条件、循环和其他语句 第6章 抽象 第7章 更加抽象 第8章 异常 第9章 魔法方法、属性和迭代器 第10章 充电时刻 第11章 文件和素材 第12章 图形用户界面 ...
Python 基础教程 第4章 Python 数据类型 Python基础教程-第4章-Python-数据类型全文共70页,当前为第1页。 Python 有 6 个主要的标准数据类型: Number(数字); String(字符串); List(列表); Tuple(元组)...
在Python中,字符串可以使用单引号、双引号或三引号来表示。字符串可以进行各种操作,如字符串连接、字符串分割、字符串检索、字符串格式化等。本文将详细介绍Python中字符串的各种操作和应用。 字符串常用操作 1....
Python基础入门教程 由浅入深讲解清晰 第3章 选择与循环 (共44页).ppt Python基础入门教程 由浅入深讲解清晰 第4章 字符串与正则表达式 (共55页).ppt Python基础入门教程 由浅入深讲解清晰 第5章 函数的设计和...
python基础教程至60课(基础) 【Python 第1课】安装 6 【Python 第2课】print 7 【Python 第3课】IDE 10 【Python 第4课】输入 12 【Python 第5课】变量 14 【Python 第6课】bool 16 【Python 第7课】if 19 【Python...
python基础教程pdf全文共3页,当前为第1页。python基础教程pdf全文共3页,当前为第1页。《Python基础教程》是2010年人民邮电出版社出版的图书, 作者是桐寺兰(MagnusLieHetland)。 python基础教程pdf全文共3页,当前...
Python基础入门教程 由浅入深讲解清晰 第3章 选择与循环 (共44页).ppt Python基础入门教程 由浅入深讲解清晰 第4章 字符串与正则表达式 (共55页).ppt Python基础入门教程 由浅入深讲解清晰 第5章 函数的设计和...
Python基础入门教程 由浅入深讲解清晰 第3章 选择与循环 (共44页).ppt Python基础入门教程 由浅入深讲解清晰 第4章 字符串与正则表达式 (共55页).ppt Python基础入门教程 由浅入深讲解清晰 第5章 函数的设计和...
python基础教程课后答案-Python基础教程(第2版) 第1章 基础知识 1 1.1 安装Python 1 1.1.1 Windows 1 1.1.2 Linux和UNIX 3 1.1.3 苹果机(Macintosh) 4 1.1.4 其他发布版 5 1.1.5 时常关注,保持更新 6 1.2 交互...
本书包括Python程序设计的方方面面,首先从Python的安装开始,随后介绍了Python的基础知识和基本概念,包括列表、元组、字符串、字典以及各种语句。然后循序渐进地介绍了一些相对高级的主题,包括抽象、异常、魔法...
第 3 章 使用字符串 .......................................41 3.1 字符串基本操作 ......................................41 3.2 设置字符串的格式:精简版 ...................41 3.3 设置字符串的格式:完整版...
第3章 列表、元组和字典的基本操作.ppt 第4章 熟练操作字符串.ppt 第5章 程序的控制结构.ppt 第6章 函数.ppt 第7章 对象与类.ppt 第8章 程序调试和异常处理.ppt 第9章 模块与类库.ppt 第10章 日期和时间.ppt 第11章 ...
本书包括Python程序设计的方方面面:首先从Python的安装开始,随后介绍了Python的基础知识和基本概念,包括列表、元组、字符串、字典以及各种语句;然后循序渐进地介绍了一些相对高级的主题,包括抽象、异常、魔法...
本书包括Python程序设计的方方面面:首先从Python的安装开始,随后介绍了Python的基础知识和基本概念,包括列表、元组、字符串、字典以及各种语句;然后循序渐进地介绍了一些相对高级的主题,包括抽象、异常、魔法...
本书包括Python程序设计的方方面面:首先从Python的安装开始,随后介绍了Python的基础知识和基本概念,包括列表、元组、字符串、字典以及各种语句;然后循序渐进地介绍了一些相对高级的主题,包括抽象、异常、魔法...