python笔记之字符串

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

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

>>> 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:返回小写字母

原文地址:https://www.cnblogs.com/girl-he/p/5118099.html