python字符串的处理
来源:互联网 发布:巨鹿之战 知乎 编辑:程序博客网 时间:2024/05/29 03:54
字符串:
通过索引访问字符串,几乎所有的通过索引来访问的情况,索引用下标表示,[]
a="hello world"
print a[0]
print a[-1]
--通过python的for range 来处理
b=" 123456789 "
a=b.strip()
total=0
for i in a:
total+=int(i)
print total
--通过字符串的下标来处理
b=" 123456789 "
a=b.strip()
total=0
for i in range(len(a)):
total+=int(a[i])
print total
字符串的切片,靠的就是字符串的索引
a="elena cathrine"
first_name=a[0:4]
print first_name+"\n"
last_name=a[6:-1]
print last_name+"\n"
打印文件的后缀
def get_ext(s):
t=s.rfind('.')
ext=s[t+1:]
return ext
通过索引访问字符串,几乎所有的通过索引来访问的情况,索引用下标表示,[]
a="hello world"
print a[0]
print a[-1]
--通过python的for range 来处理
b=" 123456789 "
a=b.strip()
total=0
for i in a:
total+=int(i)
print total
--通过字符串的下标来处理
b=" 123456789 "
a=b.strip()
total=0
for i in range(len(a)):
total+=int(a[i])
print total
字符串的切片,靠的就是字符串的索引
a="elena cathrine"
first_name=a[0:4]
print first_name+"\n"
last_name=a[6:-1]
print last_name+"\n"
打印文件的后缀
def get_ext(s):
t=s.rfind('.')
ext=s[t+1:]
return ext
print get_ext("my_notebook.html")+"\n"
dir('')可以查看所有标准字符串函数
s.find(t)
s.rfind(t)
s.index(t)
s.rindex(t)
大小写字符串
s.capitalize()
s.lower()
s.upper()
s.title()将字符串修改成驼峰结构
填充字符串
s="hello"
print s.center(10,'M')
print s.rjust(10,'M')
print s.ljust(10,'M')
除掉空格的函数
s=' hello world '
print s.strip()
s='hello world'
t=s.center(20,'M') 追加
print t
print t.strip('M') 剥离
字符串拆分函数,split 返回的是一个列表
name='elena cathrine wulka'
s=name.split(' ')
for i in s:
print i
字符串替换函数
s='hello world'
print s.replace('hello','world')
正则表达式的使用:
python的正则表达式,只能返回真或者假
import re
s='i am quit'
m=re.match(r'(.*)(done|quit)$',s)
print m.string
print m.group(2)
print m.group(1)
0 0
- python的字符串处理
- python的字符串处理
- Python 的字符串处理
- python字符串的处理
- Python的字符串处理
- python字符串的处理
- python的字符串处理函数
- python中文字符串的处理
- python对字符串的处理
- 关于python字符串的处理
- Python的字符串处理方法
- Python内置的字符串处理函数整理
- Python内置的字符串处理函数整理
- python格式化字符串中存在{}的处理
- Python内置的字符串处理函数整理
- Python内置的字符串处理函数整理
- Python内置的字符串处理函数整理
- Python内置的字符串处理函数整理
- 数据结构排序
- Java语言的接口
- JS:ajax
- Acticle 9:javascript技术--ajax基本知识
- Python中的for循环(序列、元组与字典)
- python字符串的处理
- 使用Unity3D 5.3.4 模仿2D游戏 FlappyBird(二)
- 使用servlet处理http响应
- android Fragment与Activity交互,互相发数据(附图详解)
- Ubuntu 安装mysql和 简单命令操作
- hadoop2.7.1在centos6.5下集群安装配置
- Android Studio如何使用Git提交代码到GitHub和OsChina并解决冲突
- 面试(五)
- 那些天的日记