Python字符串学习与使用

字符串是python中常用到的数据类型,总结了如下部分常用到的对字符串的操作。
len(s)
返回字符串的长度

s[2]
通过索引获取字符串中的第3个字符

s[1:5]
返回第2个到第5个字符的子字符串。注意字符串切片前面是半区间,后面是开区间。并且索引是由0开始的

s[:5]
返回第1个到第5个字符的子字符串。如果中括号内省略写分号:前的值,则表示从第一个字符开始

s[1:]
返回第2个到最后一个字符的子字符串。如果中括号内省略写分号:后的值,则表示到最后一个字符为止

+
可连结2个字符串

in
如果字符串中包含给定的字符返回True

not in
如果字符串中不包含给定的字符返回True

s.find()
返回字符串开始的索引值,否则返回-1
相关方法:rfind,index,rindex,count,startwith,endwith

s.rfind()
返回字符串最后一次出现的位置,如果没有匹配项则返回-1

s.isdigit()
如果 s只包含数字则返回 True 否则返回 False.

s.lower()
转换 s中所有大写字符为小写.
相关方法:islower, capitalize, swapcase, title, istitle, upper, isupper

str()
转换成字符串

split()
它是join的逆方法,用来将字符串分割成序列。

>>> '1+2+3+4+5'.split('+')
['1','2','3','4','5']

>>> '/usr/bin/env'.split('/')
['','usr','bin','env']

strip
strip方法返回去除两侧(不包括内部)空格的字符串。

string.encode(encoding=’UTF-8’, errors=’strict’)
以 encoding 指定的编码格式编码 string,如果出错默认报一个ValueError 的异常,除非 errors 指定的是’ignore’或者’replace’

string.decode(encoding=’UTF-8’, errors=’strict’)
以 encoding 指定的编码格式解码 string,如果出错默认报一个 ValueError 的 异 常 , 除 非 errors 指 定 的 是 ‘ignore’ 或 者’replace’

示例:

#coding:utf8
import os

strDir = "D:\\临时共享"
strName = "brand-file-name-2.7.124.124-full.45bb09bdf44d7b117dc507ad2e33ckd8.exe"

strLen = len(strName) # 获得字符串的长度等于69
print "The length is " + str(strLen)     

print "strName[0]=" + strName[0]        # 输出第1个字符 'b'
print "strName[3]=" + strName[3]        # 输出第4个字符 'n'
print "strName[1:5]=" + strName[1:5]     # 输出第2-5个字符 ‘rand’
print "strName[:5]=" + strName[:5]       # 输出第0-5个字符 'brand'
print "strName[67:]=" + strName[66:]     # 输出第67-69个字符 'exe'

print strName[0] + strName[66:]           # 输出字符 'bexe'

if "exe" in strName:                     # 判断 'exe'是否在字符串中
    print "'exe' is in " + strName

fullIndex = strName.find("full")         # 获得子串‘full'所在的位置
print "The string before 'full' is " + strName[:fullIndex]   # 输出 'brand-file-name-2.7.124.124-'

exeIndex = strName.rfind(".")   # 获得最后一个'.'所在的位置         
print "The format is " + strName[exeIndex+1:]

# 获得字符串中第一个数字
print "The first Num is " + filter(str.isdigit,strName)[0]

#识别中文编码路径
strFileName =  os.path.join(strDir.decode('utf8').encode('cp936'),strName)
print strFileName

以上示例执行结果:

The length is 69
strName[0]=b
strName[3]=n
strName[1:5]=rand
strName[:5]=brand
strName[67:]=exe
bexe
'exe' is in brand-file-name-2.7.124.124-full.45bb09bdf44d7b117dc507ad2e33ckd8.exe
The string before 'full' is brand-file-name-2.7.124.124-
The format is exe
The first Num is 2
D:\��ʱ����\brand-file-name-2.7.124.124-full.45bb09bdf44d7b117dc507ad2e33ckd8.exe

在编译器下输出中文路径显示还是乱码,但是不影响使用,如os.listdir(strDir.decode(‘utf8’).encode(‘cp936’))还是可以识别到路径并列出文件夹下的文件名称的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章