您的位置:首页 > 编程语言 > Python开发

Python基础教程 第3章: 使用字符串 学习笔记

2015-12-16 11:30 786 查看
所有标准的序列操作(索引,分片,乘法,判断成员资格,求长度,最大值,最小值)对字符串同样适用。

字符串的格式化:

#格式化一个参数
format = "Hello %s"
value = "123"
print format % value

#格式化多个参数
#values必须为元组,列表的话会报错
format = "Hello %s and %s and %s"
values = ("1", "2", "3")
print format % values

format = "Hello %d and %s and %f and %x"
v = (10, "ww", 3.14, 123)
print format % v



>>>
Hello 123
Hello 1 and 2 and 3
Hello 10 and ww and 3.140000 and 7b


Python的格式化符跟C语言的差不多,其中%s, 使用str转换任意Python对象,%r,使用repr转换任意Python对象。

val = 1.2222222222222222222

#字段宽度为10
print "%10f" % val

#字段宽度为10,精度为2
print "%10.2f" % val

print "Hello %.5s" % "111111111111111111111111"

'''
在字段宽度和精度之前可以放一个标志, 可以是0, +号, -号或空格
0    --> 表示数字将会用0进行填充
-号      --> 表示用来左对齐数值
+号      --> 表示不管是正数还是负数都会标示出符号
空格      --> 表示在正数前面加上空格,在正负数对齐的时会很有用
'''
print "%010.2f" % val
print "%-10.2f" % val
print "%+10.2f" % val
print "%+10.2f" % -10.221212111111111111
print "% d" % 10
print "% d" % -10
print "%d" % 10
print "%d" % -10



>>>
1.222222
1.22
Hello 11111
0000001.22
1.22
+1.22
-10.22
10
-10
10
-10



format = "%-*s%s"
val = (10, "Hello", "123")
print format % val

'''
-表示左对齐
*号表示从元组中读取字段
%s 字符串
%s 字符串

%-10s%s
左对齐,宽度为10,第一个字符串Hello  第二个字符串123

%x.xs
.之前的x表示宽度
.之后的x表示精度
'''



>>>
Hello 123



字符串方法:

①. 查找子串,如果找到则返回子串所在位置的最左端索引,否则返回-1

str = "Hello Python, www.qq.com www.baidu.com kokomsl 123 www.qq.com www.baidu.com"
nIndex = str.find("www.qq.com")
print nIndex
str1 = str[nIndex:nIndex+len("www.qq.com")]
print str1
nIndex = str.find("www.aa.com")
print nIndex



>>>
14
www.qq.com
-1



str = "Hello Python, www.qq.com www.baidu.com kokomsl 123 www.qq.com www.baidu.com"

#还可以指定开始查找的起点和终点
#只提供起点
print str.find("www.qq.com", 20)

#提供起点和终点
#终点超过字符串长度应该是按照字符串的长度来的
print str.find("www1.qq.com", 20, 10000)



>>>
51
-1


②. 连接序列中的元素,被连接的序列元素都必须是字符串。

str1 = ["1", "2", "3", "4"]
str2 = ("10", "20", "40", "50")
val = "+"
print val.join(str1)
print val.join(str2)

str3 = "", "usr", "bin", "env"
print "/".join(str3)

str4 = "C", "Windows", "system32", ""
print "\\".join(str4)



>>>
1+2+3+4
10+20+40+50
/usr/bin/env
C\Windows\system32\


③. 返回字符串的小写字母版

str = "HelloWOrLD, Hehe, Python"
print str

#不修改原始序列,通过返回值返回修改好的新序列
str1 = str.lower()
print str
print str1

#字符串中所有单词首字母大写
str = 'hello world mytest hehe haha'

#也是通过返回值返回
str1 = str.title()
print str
print str1

#使用string模块中的方法
import string
str = 'zxvas ada sgfdg asd ada asd'
str1 = string.capwords(str)
print str
print str1



>>>
HelloWOrLD, Hehe, Python
HelloWOrLD, Hehe, Python
helloworld, hehe, python
hello world mytest hehe haha
Hello World Mytest Hehe Haha
zxvas ada sgfdg asd ada asd
Zxvas Ada Sgfdg Asd Ada Asd


④. 查找替换,返回字符串中所有匹配项均被替换之后得到的字符串。

str = "Hello Python, www.qq.com www.baidu.com www.qq.com www.baidu.com www.qq.com"
print str

str1 = str.replace("www.qq.com", "123")
print str1



>>>
Hello Python, www.qq.com www.baidu.com www.qq.com www.baidu.com www.qq.com
Hello Python, 123 www.baidu.com 123 www.baidu.com 123


⑤. 分割字符串, 它是join的逆方法,用来将字符串分割。

str = "admin|admin888|www.baidu.com|www.qq.com|python   | hello a a ||123|"
print str

str1 = str.split("|")
print str
print str1



>>>
admin|admin888|www.baidu.com|www.qq.com|python | hello a a ||123|
admin|admin888|www.baidu.com|www.qq.com|python | hello a a ||123|
['admin', 'admin888', 'www.baidu.com', 'www.qq.com', 'python ', ' hello a a ', '', '123', '']


⑥. 去除字符串两侧的指定的信息并返回

str = " adss ada asd asda   asd   "
print str

#默认为去除两侧的空格
str1 = str.strip()
print str
print str1

#指定去除两侧的字符串
str = "123Helloworodl ad sss123"
str1 = str.strip("123")
print str
print str1



>>>
adss ada asd asda asd
adss ada asd asda asd
adss ada asd asda asd
123Helloworodl ad sss123
Helloworodl ad sss


⑦. 查找替换字符串中的字符并返回新字符串

'''
在使用translate转换之前,需要先完成一张转换表,转换表中是以某字符替换某字符的对应关系。
这个表有多大256个项目,可以使用string模块中的maketrans函数生成。
maketrans函数接收两个参数,两个登场的字符串,表示第一个字符串中的每个字符都用第二个字符串
中相同位置的字符替换。
'''

from string import maketrans

#生成替换表
#c -> k
#s -> z
table = maketrans("cs", "kj")
print len(table)

print table[97:123]
table = maketrans("","")[97:123]
print table

table = maketrans("cs", "kj")
str = "zccslssscc"
print str
str1 = str.translate(table)
print str
print str1

#translate还有第二个参数,这个参数是可选的
#这个参数用来指定需要删除的字符
str1 = str.translate(table, "c")
print str1



>>>
256
abkdefghijklmnopqrjtuvwxyz
abcdefghijklmnopqrstuvwxyz
zccslssscc
zccslssscc
zkkjljjjkk
zjljjj



import string

#检查字符串是否由数字组成
str = "12345678"
print str.isdigit()
str = "12345678a"
print str.isdigit()

#检查字符串是否由字母字符组成
print "assada".isalpha()
print "assada1".isalpha()

#检查字符串是否由字母或数字字符组成
print "assada".isalnum()
print "assada1".isalnum()
print "@!#!@#!@".isalnum()



>>>
True
False
True
False
True
True
False

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: