码迷,mamicode.com
首页 > 编程语言 > 详细

python入门(二)字符串的处理

时间:2018-03-30 13:15:02      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:python入门

python常见的数据类型
a= 10 整数型
b=10.0 浮点型
c=“hello” 字符串
d=True 布尔 True/Fales

应用方法
整数:
a=1
print (a)
浮点数:
b=2.0
print (b)
布尔:
print (b>a)
True
print (b<a)
Fales
字符串:
c="hello,tom"
查找:
print c.find(‘h‘)
返回下标
0
print c.find(‘o‘)
4
print c.find(‘w‘)
查找不到返回
-1
替换
print c.replace(‘tom‘,‘lili‘)
hello,lili
分割
print c.split (‘,‘);
以逗号问分隔符
"hello","tom"
集成整合
c=":"
d=("hello","tom")
print c.join(d);
hello:tom
前后去空格
c=" hello,tom "
print c.strip();
hello,tom
字符串格式化
c=“hello,tom”
{0}.format(c)
hello,tom
{0}{1}.format("hello","tom")
hello tom
{1}{0}.format("hello","tom")
tom hello


列表list[]
列表可以把任何字符串,数字,字母加入,列表中的元素间没有任何的关联,列表冲存在下标,默认从0开始
比如:
conn=["1","1.11","hello","True"] 这就是一个列表
列表常用的处理方法:


c=conn.append("lili") 在列表最后添加一个元素
print (c)
1,1.11,hello,True,lili


c=conn.pop() 在列表最后删除一个元素
print (c)
1,1.11,hello


conn.index() 返回元素的下标
print conn.index("hello")
2


conn.remove() 根据下标删除元素
同上


conn.sort() 排序
conn.reverse() 反向排序


conn.[:] 分片,前开后闭
a=conn[1:3]
b=conn[:3]
c=conn[2:]
d=conn[:-1]
print a
[1.11,hello]
print b
[1,1.11,hello]
print c
[hello,True]
print d
[1,1.11,hello]

python入门(二)字符串的处理

标签:python入门

原文地址:http://blog.51cto.com/13654063/2092891

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!