python 字符串(str)和列表(list)的互相轉換

1.str >>>list google

 

 
  1. str1 = "12345".net

  2. list1 = list(str1)code

  3. print list1blog

  4.  
  5. str2 = "123 sjhid dhi"it

  6. list2 = str2.split() #or list2 = str2.split(" ")co

  7. print list2join

  8.  
  9. str3 = "www.google.com"

  10. list3 = str3.split(".")

  11. print list3

  12.  
輸出爲:
 
  1.  
  2. ['1', '2', '3', '4', '5']

  3. ['123', 'sjhid', 'dhi']

  4. ['www', 'google', 'com']

3.list >>>str

 

 
  1. str4 = "".join(list3)

  2. print str4

  3. str5 = ".".join(list3)

  4. print str5

  5. str6 = " ".join(list3)

  6. print str6

 

輸出爲:

 

 
  1. wwwgooglecom

  2. www.google.com

  3. www google com


 

參考連接:

http://blog.csdn.net/cnmcsdn456/article/details/27830671

http://piziyin.blog.51cto.com/2391349/568426

相關文章
相關標籤/搜索