python中的字符串操作

本文深入解析了Python中字符串处理的基本方法,包括字符串的大小写转换、查找、编码、解码、分割、连接、长度计算等核心操作。通过具体示例展示每个方法的功能和使用场景,帮助读者快速掌握字符串处理技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/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\t 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 = '\t'
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('\n'))
按照行进行分割
'''
str = "line1-a b c\nline2- 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值