Python字符串基础操作

简介: 找到我就是缘分,为技术一起努力!格式符#price_width = 10item_width = width - price_widthheader_format = '%-*s%*s'format = '%-*s%*.

找到我就是缘分,为技术一起努力!

格式符

#
price_width = 10
item_width = width - price_width

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)
find(str,start,end):从start开始end结束寻找与str相匹配的字符,找到返回匹配项index,找不到返回-1
a='With a moo-moo here. and a moo-moo there'.find('moo',7)
>>> print(a)
7

>>> a='With a moo-moo here. and a moo-moo there'.find('moo',1,8)
>>> print(a)
-1
join():在字符序列之间加入分隔符
>>> seq = ['1','2','3','4','5']
>>> sep
'+'
>>> sep.join(seq)
'1+2+3+4+5'
lower():全部转为小写
>>> 'ZDF'.lower()
'zdf'
title():首字母大写
>>> 'zdf'.title()
'Zdf'
replace(oldstr , newstr):替换字符
>>> 'zdf is a clever boy!'.replace('clever','handsome')
'zdf is a handsome boy!'
split(/PATTERN):以模式来分割,模式的意思就是REGEX
>>> 'zdf+zdf+zdf'.split('+')
['zdf', 'zdf', 'zdf']
strip():去除两侧空格(不包括内部的)
>>> '      zdf         '.strip()
'zdf'
strip('*'):也可指定去除两侧特定的字符 ,如*
>>> '*********zdf****************'.strip('*')
'zdf'
目录
相关文章
|
12天前
|
Python
1167: 分离字符串(PYTHON)
1167: 分离字符串(PYTHON)
|
30天前
|
大数据 Python
使用Python查找字符串中包含的多个元素
本文介绍了Python中查找字符串子串的方法,从基础的`in`关键字到使用循环和条件判断处理多个子串,再到利用正则表达式`re模块`进行复杂模式匹配。文中通过实例展示了如何提取用户信息字符串中的用户名、邮箱和电话号码,并提出了优化策略,如预编译正则表达式和使用生成器处理大数据。
20 1
|
1月前
|
数据挖掘 开发者 Python
Python:字符串判断子串
Python:字符串判断子串
|
1月前
|
程序员 数据安全/隐私保护 Python
Python:翻转字符串
Python:翻转字符串
|
1月前
|
索引 Python
Python系列(14)—— 字符串运算符
Python系列(14)—— 字符串运算符
|
1月前
|
存储 自然语言处理 数据挖掘
Python:计算字符串中每个单词出现的次数
Python:计算字符串中每个单词出现的次数
|
3天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
25 0
|
3天前
|
程序员 索引 Python
06-python数据容器-set(集合)入门基础操作
06-python数据容器-set(集合)入门基础操作
|
4天前
|
数据采集 Python
python学习9-字符串
python学习9-字符串
|
12天前
|
Java 索引 Python
Python标准数据类型-字符串常用方法(下)
Python标准数据类型-字符串常用方法(下)
19 1