Python基础教程之第3章 使用字符串

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!"#$%&\'()*+,-./:;<=>[email protected][\\]^_`{|}~ \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
#===================================
时间: 2024-08-24 08:40:55

Python基础教程之第3章 使用字符串的相关文章

Python基础教程之第2章 列表和元组

D:\>python Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. #2.1序列概览 >>> edward=['Edward Gumby', 4

Python基础教程之第5章 条件, 循环和其它语句

Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 #Chapter 5 条件, 循环和其它语句 #5.1 print和import的很多其它信息 #对于非常多应用程序来说, 使用logging模块记日志比print语句更合适 #5.1.1 使用逗号输出 #能够看到, 每隔參数之间都自己主动插入了一个空格符 >>> print 'Age:',42 Age: 42 >&g

Python基础教程之List对象 转

Python基础教程之List对象 时间:2014-01-19    来源:服务器之家    投稿:root 1.PyListObject对象typedef struct {    PyObject_VAR_HEAD    PyObject **ob_item;    Py_ssize_t allocated;} PyListObject; PyObject_VAR_HEAD中的obsize表示该list对象含有的元素个数, 而allocated表示该list对象占用的内存空间. ob_item

Python基础教程之import和from...import

一般使用import和from...import...导入模块. 以下述spam.py内的文件代码为例. ''' 遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书! ''' # spam.py print('from the spam.py') money = 1000 def read1(): print('spam模块:', money) def read2(): print('spam

《python基础教程》第3章使用字符串 读书笔记

第三章:使用字符串 1.字符串格式化操作符是一个百分号 % 2.只有元组和字典可以格式化一个以上的值.列表或者其他序列只会被解释为一个值. 3.in操作符只能查找字符串中的单个字符. 4.字符串方法: ①find():find方法可以在一个较长的字符串中查找子串,它返回子串所在位置的最左端索引,如果没有找到则返回-1.这个方法还能提供起始点和结束点的范围(提供第二,第三个参数),范围包含第一个索引,但不包含第二个索引,这在python中是个惯例. ②join():这个方法用来连接序列中的元素(序

Python核心编程基础教程之Python运算符、运算符优先级、表达式简介--20150717

Python核心编程基础教程之Python运算符.运算符优先级.表达式简介 1.Python运算符与表达式: (1)认识Pyhton运算符1:什么是运算符 在Python运算中,有时候我们需要对一个或者多个数字或者一个或者多个字符串进行运算操作,*,+ (2)认识Pyhton运算符2:运算符有哪些以及运算符的使用方法 + :加 - :减 * :乘 / :除 ** :幂 < :小于 > :大于 != :不等于 // :求相除的整数部分 % :求相除的余数部分 & :按位与 | :按位或

OpenVAS漏洞扫描基础教程之OpenVAS概述及安装及配置OpenVAS服务

OpenVAS漏洞扫描基础教程之OpenVAS概述及安装及配置OpenVAS服务 OpenVAS基础知识 OpenVAS(Open Vulnerability Assessment System)是开放式漏洞评估系统,其核心部分是一个服务器.该服务器包括一套网络漏洞测试程序,可以检测远程系统和应用程序中的安全问题.OpenVAS不同与传统的漏洞扫描软件.所有的OpenVAS软件都是免费的,而且还采用了Nessus(一款强大的网络扫描工具)较早版本的一些开放插件.虽然Nessus很强大,但是该工具

Linux入门基础教程之Linux下软件安装

Linux入门基础教程之Linux下软件安装 一.在线安装: sudo apt-get install 即可安装 如果在安装完后无法用Tab键补全命令,可以执行: source ~/.zshrc APT(Advanced Packaging Tool), 包括apt-get, apt-cache, apt-cdrom等工具,APT可以自动下载,配置,安装二进制或者源代码格式的软件包,因此简化了Unix系统上管理软件的过程,Ubuntu是Debian的发行版.Debian使用的包管理工具是dpkg

python基础教程_学习笔记5:字符串

字符串 基本字符串操作 字符串也是序列,因此序列的基本操作(索引.分片.连接.乘法.长度.求最大值和最小值.成员资格)对字符串同样适用: 索引 >>> 'a_string'[0] 'a' 长度 >>> len('a_string') 8 求最大值 >>> max('a_string') 't' 求最小值 >>> min('a_string') '_' 乘法 >>> 'a_string'*2 'a_stringa_st