Python 列表 元組 字串相互轉換

2021-10-08 11:19:51 字數 2862 閱讀 7571

我們直接上**:

字串和元組轉換成列表:

example1 =

'6666666'

print

(example1)

print

(type

(example1)

)list1 =

list

(example1)

print

(list1)

print

(type

(list1)

)example2 =(1

,2,3

,4,5

)print

(example2)

print

(type

(example2)

)list2 =

list

(example2)

print

(list2)

print

(type

(list2))-

----

----

--結果如下---

----

---6666666

<

class

'str'

>

['6'

,'6'

,'6'

,'6'

,'6'

,'6'

,'6'

]<

class

'list'

>(1

,2,3

,4,5

)<

class

'tuple'

>[1

,2,3

,4,5

]<

class

'list'

>

列表、字串轉換成元組:

example1 =

'6666666'

print

(example1)

print

(type

(example1)

)tuple1 =

tuple

(example1)

print

(tuple1)

print

(type

(tuple1)

)example2 =[1

,2,3

,4,5

]print

(example2)

print

(type

(example2)

)tuple2 =

tuple

(example2)

print

(tuple2)

print

(type

(tuple2))-

----

----

--結果如下---

----

----

---6666666

<

class

'str'

>

('6'

,'6'

,'6'

,'6'

,'6'

,'6'

,'6'

)<

class

'tuple'

>[1

,2,3

,4,5

]<

class

'list'

>(1

,2,3

,4,5

)<

class

'tuple'

>

列表、元組轉換成字串:

example1 =

['1'

,'2'

,'3'

,'4'

,'5'

,'6'

]print

(example1)

print

(type

(example1)

)string1 =

",".join(example1)

print

(string1)

print

(type

(string1)

)example2 =

('1'

,'2'

,'3'

,'4'

,'5'

,'6'

)print

(example2)

print

(type

(example2)

)string2 =

",".join(example2)

print

(string2)

print

(type

(string2))-

----

----結果如下---

----

----

-['1',

'2',

'3',

'4',

'5',

'6']

<

class

'list'

>1,

2,3,

4,5,

6<

class

'str'

>

('1'

,'2'

,'3'

,'4'

,'5'

,'6'

)<

class

'tuple'

>1,

2,3,

4,5,

6<

class

'str'

>

python列表 元組 字串

python中列表 元組 字串均為可迭代物件 lst 1 2,3 4 for i in lst print i,end print tu 1 2,3 4 for i in tu print i,end print str 1234 for i in str print i,end print 1 2...

python 列表 元組 字串

列表新增 list.extend list.insert 列表刪除 list.remove 刪除某乙個元素 list.pop 刪除某乙個返回刪除的哪乙個,預設刪除最後乙個 del list 這是個語句也可以刪除整個元素,加括號刪除某乙個元素 列表分片 list 1 3 從第二個到第四個切割,並顯示出...

python中列表,元組,字典,字串相互轉換

1 字典 dict 字典轉為字串,返回 print type str dict str dict 字典可以轉為元組,返回 age name class print tuple dict 字典可以轉為元組,返回 7,zara first print tuple dict.values 字典轉為列表,返...