列表,元组,字符串的相互转换:

将字符串转换为序列和元组:

>>> s="hello"

>>> list(s)
['h', 'e', 'l', 'l', 'o']
>>> tuple(s)
('h', 'e', 'l', 'l', 'o')
>>> list(tuple(s))
['h', 'e', 'l', 'l', 'o']
>>> tuple(list(s))
('h', 'e', 'l', 'l', 'o')

将序列和元组转换为字符串:

>>> "".join(list(s))
'hello'
>>> "".join(tuple(s))
'hello'
>>> str(s)
'hello'
>>> str(list(s))
"['h', 'e', 'l', 'l', 'o']"
>>> str(tuple(s))
"('h', 'e', 'l', 'l', 'o')"

 

 

字符串方法:

(1)find:返回子字符串第一次出现最左端索引的位置

>>> s="hello"
>>> s.find("l")
2

(2)join

(3)lower:返回小写字母

 

posted on 2016-01-10 11:32  荷繁  阅读(208)  评论(0编辑  收藏  举报