python中的字符串操作

#!/usr/bin/python
# -*- coding: UTF-8 -*-

'''
str.capitalize()
'''
str = 'this is a string example'
print str.capitalize()

'''
str.center(width[, fillchar])
'''
str = '123'
print str.center(10, '0')

'''
str.count(sub, start=0, end=len(string))
'''
str = '123abc123'
print str.count('123')

'''
str.decode(encoding='utf-8', errors='strict')
'''
str = '123'
print str.encode('base64', 'strict')

'''
str.encode(encoding='utf-8', errors='strict')
'''
str = 'MTIz'
print str.decode('base64', 'strict')

'''
str.endswith(suffix[, start[, end]])
'''
str = '123abc'
print str.endswith('abc', 0, len(str))


'''
str.expandtabs(tabsize=8)
'''
str = "this is	 a example"
print str.expandtabs(8)

'''
str.find(str, beg=0, end=len(string))
'''
str = '0123456789'
print str.find('345')

'''
str.index(str, beg=0, end=len(string))
只不过如果str不在string中会报一个异常
'''
str = '123'
print str.index('23')

'''
str.isalnum()
'''
str = 'abc123'
print str.isalnum()

'''
str.isalpha()
'''
str = 'abc123'
print str.isalpha()

'''
str.isdigit()
'''
str='123'
print str.isdigit()

'''
str.islower()
'''
str = 'abc'
print str.islower()

'''
str.isupper()
'''
str = 'BASIC SEARCH'
print str.isupper()

'''
str.isnumeric()
这种方法只针对unicode对象
'''
str = u"this2009"
print str.isnumeric()

'''
str.isspace()
'''
str = '	'
print str.isspace()

'''
str.istitle()
'''
str = 'This Is Example'
print str.istitle()

'''
str.join(sequence)
'''
str = '-'
seq = ['a', 'b', 'c']
print str.join(seq)

'''
str.ljust(width[, fillchar])
'''
str = 'abc'
print str.ljust(10, '-')
print str.rjust(10, '-')

'''
str.lower()
'''
str = 'THIS is example'
print str.lower()

'''
str.lstrip([chars])
'''
str = '123abc123'
print str.lstrip('123')
print str.rstrip('123')
print str.strip('123')

'''
maketrans(intab, outtab)
创建字符映射的转换表
'''
#print maketrans('abc', '123')

'''
max(str)
返回字符串中最大的字母
'''
str = 'abc'
print max(str)

'''
min(str)
返回字符串中最小的字母
'''
str = 'abc'
print min(str)

'''
str.partition(str)
根据指定字符进行分割

返回三元的元组
'''
str='a|b|c'
print str.partition('|')

'''
str.replace(old, new[, max])
'''
str = 'abc123abc'
print str.replace('123', 'abc', 1)

'''
str.split(str="", num=string.count(str))
'''
str = 'a|b|c'
print str.split('|')

'''
str.splitlines(num = string.count('
'))
按照行进行分割
'''
str = "line1-a b c
line2- 1 2 3"
print str.splitlines(0)

'''
str.startwith(str, beg = 0, end = len(str))
'''
str = 'a123'
print str.startswith('a')

'''
str.swapcase()
'''
str = 'this is example A'
print str.swapcase()

'''
str.title()
'''
str = 'this is example'
print str.title()

'''
str.translate(table[, deletechars])
'''
trantab = {
        '1':'a',
        '2':'b',
        '3':'c'
}
str = '123abc'
#print str.translate(trantab)

'''
str.upper()
'''
原文地址:https://www.cnblogs.com/bai-jimmy/p/5302261.html