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

Python中的列表,元组,字符串之间的相互转化

时间:2015-04-11 11:38:01      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

  Python中的列表元组和字符串之间的相互转化需要利用,tuple(),list(),str().

示例如下:

>>> the_string = "hello I‘am xiaoli!"
>>> #字符串转化为元组
>>> the_tuple = tuple(the_string)
>>> the_tuple
(h, e, l, l, o,  , I, "", a, m,  , x, i, a, o, l, i, !)
>>> #字符串转化为列表
>>> the_list = list(the_string)
>>> the_list
[h, e, l, l, o,  , I, "", a, m,  , x, i, a, o, l, i, !]
>>> #元组转化为列表
>>> the_list = list(the_tuple)
>>> the_list
[h, e, l, l, o,  , I, "", a, m,  , x, i, a, o, l, i, !]>>> the_tuple = tuple(the_list)
>>> the_tuple
(h, e, l, l, o,  , I, "", a, m,  , x, i, a, o, l, i, !)
>>> #如果将元组和列表转化为字符串需要join()
>>> "".join(the_tuple)
"hello I‘am xiaoli!"
>>> "".join(the_list)
"hello I‘am xiaoli!"
>>> #如果不用join()函数
>>> str(the_tuple)
(\‘h\‘, \‘e\‘, \‘l\‘, \‘l\‘, \‘o\‘, \‘ \‘, \‘I\‘, "\‘", \‘a\‘, \‘m\‘, \‘ \‘, \‘x\‘, \‘i\‘, \‘a\‘, \‘o\‘, \‘l\‘, \‘i\‘, \‘!\‘)

总结一点:join是连接字符的重要方法。

 

Python中的列表,元组,字符串之间的相互转化

标签:

原文地址:http://www.cnblogs.com/xiaoli2018/p/4417184.html

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