《Python学习手册》(三)
string
字符串,和列表、元组,都适用序列操作。
迭代
for x in S: print(x)
[c * 2 for c in S]
Comparasion:
>>> title = "mean"'of'"lid"
>>> title
'meanoflid'
>>> title = "mean",'of',"lid"
>>> title
('mean', 'of', 'lid')
python3中如何print不换行:
>>> myjob = "hacker"
>>> for c in myjob: print(c, end=' ')
...
h a c k e r >>>
索引和分片:通过[]进行操作
扩展分片:s[x:y:z],索引s中的元素,从偏移x到偏移为y-1,每隔z个元素索引一次
>>> s = 'abcdefghijklmnopqrstuvwxyz'
>>> s[1:20:2]
'bdfhjlnprt'
>>> s = 'hello'
>>> s[::-1]
'olleh'
>>> s = 'abcedfg'
>>> s[5:1:-1]
'fdec'
sys中的argv属性: non-understanding
chr() & ord() #ASCII码与字符转换
str() & int() or float() #数转串 & 串转数
内置eval函数: non-understanding
一些方法调用:
( 它们都不会改变字符串s )
'X' in S
s.find('pa') # 返回字串位置,若无返回-1
s.rstrip() # 移除末尾字符(默认是空格)
s.replace('a', 'b') # replace all
s.replace('a', 'b', k) # replace k times
s.split([',',[2]]) # be split by ',' for 2 times
s.isdigit() # 或是isalpha
s.lower() # 或是upper
s.endswith('spam') # 或是startswith
'spam'.join(s) # 用‘spam’将s所有字符连接
'X'.join([S1, S2, S3]) # 用‘X’将S1, S2, S3连接
若需要对一个超长字符串进行许多的修改,为了优化脚本的性能,可能需要将字符串转换为一个支持原处修改的对象。例如:list函数
>>> S = 'spammy'
>>> L = list(S)
>>> L[3] = 'x'
>>> S = ''.join(L)
格式化表达式:
"%a %s parrot" % kind
"a {0} parrot".format(kind)
'That is %d %s bird!' % (1, 'dead')
'That is {0} {1} bird!'.format(1, 'dead')
'%s -- %s -- %s' % (42, 3,14159, [1, 2, 3])
#所有类型都可以转换为字符串,故都用%s也是正确的
'%.*f' % (4, 1/3.0)
#用*指定width和precision,它们的值从%运算符右边的下一项获取
details : http://www.cnblogs.com/vamei/archive/2013/03/12/2954938.html
基于字典的字符串格式化
>>> "%(n)d %(x)s" % {"n":1, "x":"spam"}
'1 spam'
>>> reply = """
Greetings...
Hello %(name)s!
Your age squared is %(age)s
"""
>>> values = {'name': 'Bob', 'age': 40}
>>>print(reply % values)
内置函数vars:
>>> food = 'spam'
>>> age = 40
>>> vars()
{'food':'spam', 'age':40, ...many more...}
>>> "%(age)d %(food)s" % vars()
'40 spam'
format方法
在主体字符串中,花括号通过位置({1})或关键字({food})指出替换目标及将要插入的参数。
>>> template = '{0}, {1} and {2}'
>>> template.format('spam', 'ham', 'eggs')
'spam, ham and eggs'
>>> template = '{motto}, {pork} and {food}'
>>> template.format(motto='spam', pork='ham', food='eggs')
'spam, ham and eggs'
>>> '{motto}, {0} and {food}'.format(42, motto=3.14, food=[1, 2])
'3.14, 42 and [1, 2]'
格式化字符串可以指定对象属性和字典键,方括号指定字典键,点表示位置或关键字所引用的一项的对象属性。
>>> import sys
>>> 'My {1[spam]} runs {0.platform}'.format(sys, {'spam':'laptop'})
'My laptop runs darwin'
>>> 'My {config[spam]} runs {sys.platform}'.format(sys=sys,
config={'spam':'laprop'})
'My laptop runs darwin'
方括号可以指定列表(及其他的序列)偏移量以执行索引。
>>> somelist = list('spam')
>>> 'first = {0[0]}, third = {0[2]}'.format(somelist)
'first = s, third = a'
>>> 'first = {0}, last = {1}'.format(somelist[0], somelist[-1])
'first = s, last = m'
>>> parts = somelist[0], somelist[-1], somelist[1:3]
>>> 'first = {0}, last = {1}, middle = {2}'.format(*parts) ##
"first = s, last = m, middle = ['p', 'a']"
??parts前为什么要有 ' * ' , 是什么意思
添加具体格式化
{fieldname!conversionflag:formatspec}
fieldname: 指定参数的一个数字或关键字,后面跟着可选的“.name”或“[index]”成分引用
conversionflag: 对repr、str、ascii内置函数的调用,用r、s、a表示
formatspec: 包括字段宽度、对齐方式、补零、小数点精度等细节
>>> '{0:>10} = {1:<10}'.format('spam', 123.4567)
' spam = 123.4567 '
>>> '{0.platform:>10} = {1[item]:<10'.format(sys, dict(item='laptop'))
' darwin = laptop '
>>> '{0:e}, {1:.3e}, {2:g}'.format(3.14159, 3.14159, 3.14159)
'3.141590e+00, 3.142e+00, 3.14159'
>>> '{0:f}, {1:.2f}, {2:06.2f}'.format(3.14159, 3.14159, 3.14159)
'3.141590, 3.14, 003.14'
>>> '{0:X}, {1:o}, {2:b}'.format(255, 255, 255)
'FF, 377, 111111111'
>>> '{0:.{1}f}'.format(1 / 3.0, 4)
'0.3333'
>>> '%.*f' % (4, 1 / 3.0)
'0.3333'
>>> format(1.2345, '.2f')
'1.23'
《Python学习手册》(三)的更多相关文章
- 读书分享全网学习资源大合集,推荐Python学习手册等三本书「01」
0.前言 在此之前,我已经为准备学习python的小白同学们准备了轻量级但超无敌的python开发利器之visio studio code使用入门系列.详见 1.PYTHON开发利器之VS Code之 ...
- 《Python学习手册》读书笔记
之前为了编写一个svm分词的程序而简单学了下Python,觉得Python很好用,想深入并系统学习一下,了解一些机制,因此开始阅读<Python学习手册(第三版)>.如果只是想快速入门,我 ...
- 《Python学习手册》读书笔记【转载】
转载:http://www.cnblogs.com/wuyuegb2312/archive/2013/02/26/2910908.html 之前为了编写一个svm分词的程序而简单学了下Python,觉 ...
- 《Python学习手册 第五版》 -第11章 赋值、表达式和打印
上一章对Python的语句和语法已经进行了基本的说明,接下来就是每个章节的详细说明,本章的主要内容就是标题中涵盖的三点:赋值语句.表达式语句.打印语句 本章重点内容如下: 1.赋值语句 1)赋值语句的 ...
- 《Python学习手册 第五版》 -第10章 Python语句简介
前面在开始讲解数据类型的时候,有说过Python的知识结构,在此重温一下 Python知识结构: 程序由模块组成 模块包含语句 语句包含表达式 表达式创建并处理对象 关于知识结构,前面已经说过我自己的 ...
- 《Python学习手册 第五版》 -第18章 参数
在函数的定义和调用中,参数是使用最多喝最频繁的,本章内容就是围绕函数的参数进行讲解 本章重点内容如下: 1.参数的传递 1)不可变得参数传递 2)可变得参数传递 2.参数的匹配模式 1)位置次序:从左 ...
- 《Python学习手册 第五版》 -第15章 文档
本章主要介绍Python中的文档,会通过多种方式来说明,如果查看Python自带文档和其他参考的资料 本章重点内容 1.#注释:源文件文档 2.dir函数:以列表显示对象中可用的属性 3.文档字符串 ...
- 《Python学习手册 第五版》 -第16章 函数基础
前面的章节讲解的是一些基础数据类型.基本语句使用和一些文档查看的内容,这些都是一些基础,其实还谈不上入门,只有了解了函数,才算入门 函数是编程里面使用最多的也是最基本的程序结构, 本章重点内容 1.函 ...
- Python学习手册(第4版) - 专业程序员的养成完整版PDF免费下载_百度云盘
Python学习手册(第4版) - 专业程序员的养成完整版PDF免费下载_百度云盘 提取码:g7v1 作者简介 作为全球Python培训界的领军人物,<Python学习手册:第4版>作者M ...
- Python学习手册(第4版)PDF高清完整版免费下载|百度云盘
Python学习手册(第4版)PDF高清完整版免费下载|百度云盘 提取码:z6il 内容简介 Google和YouTube由于Python的高可适应性.易于维护以及适合于快速开发而采用它.如果你想要编 ...
随机推荐
- IOS . -转载-10行代码搞定九宫格
//每个Item宽高 CGFloat W = ; CGFloat H = ; //每行列数 NSInteger rank = ; //每列间距 CGFloat rankMargin = (self.v ...
- 同时调整lv分区的大小(减少一个,增加另一个)
author:headsen chen date: 2018-04-20 16:48:06 1.查看分区:/home 为67G,太大了,/ 是50g,太小了. [root@localhost ~]# ...
- Leetcode-Construct Binary Tree from inorder and postorder travesal
Given inorder and postorder traversal of a tree, construct the binary tree. Solution: /** * Definiti ...
- 推荐一个CSS类库
animate.css 一个封装好的动画效果类
- c# + Sql server 事务处理
事务(Transaction)是并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位. 通过事务,SQL Server能将逻辑相关的一组操作绑定在一起,以便 ...
- sql---如何把sql查询出来的结果当做另一个sql的条件查询,1、语句2、with as
'; -- table2 的 name 作为 table1的条件 select * from table1 where name in (select name from table2) --如果有多 ...
- Powershell&.NET数值取整处理
如何取一个数的整数值? 使用类型强制转换 Powershell的强制转换有2种方式,一种是直接类型强制转换,另一种是通过-as运算符进行转换 PS F:\> [int] (3 / 2) # 直接 ...
- 2.Access the mongo Shell Help-官方文档摘录
总结: 1.使用help可以查看帮助信息db.help() help等 2.查看对应的实现方法.比如 test@gzxkvm52$ db.updateUser function (name, upd ...
- django博客项目11
.....................
- 查询dubbo服务
1.公司内网肯定会有服务治理平台,把自己提供的服务接口当关键字查询即可. 2.命令方式实现 查看本机Dubbo服务是否启动,telnet localhost [端口号],端口号是在注册dubbo服务的 ...