python的read、write方法的操作对象都是string。输入、输出和逻辑业务上很多时候都要用到string、list互转。

1.简单用法

import string
str = 'abcde'
list = list(str)
print list
# ['a', 'b', 'c', 'd', 'e']
str_convert = ''.join(list)
print str_convert
# 'abcde'

2.使用split()将一个字符串分裂成多个字符串组成的列表。

str2 = "abc grt werwe"
list2 = str2.split() # or list2 = str2.split(" ")
print list2
# ['abc', 'grt', 'werwe']

str3 = "www.google.com"
list3 = str3.split(".")
print list3
# ['www', 'google', 'com']

3.使用 strip() 方法移除字符串头尾指定的字符。

ids = '1001,1002,1003,1004,'
ids_list = ids.strip(',').split(',')
print ids_list
# ['1001', '1002', '1003', '1004']

4.使用自定字符连接list中的字符串组成一个新字符串

list3 = ['www', 'google', 'com']
str4 = "".join(list3)
print str4
# wwwgooglecom
str5 = ".".join(list3)
print str5
# www.google.com
str6 = " ".join(list3)
print str6
# www google com

done!

python string/list转换的更多相关文章

  1. 【python】bytearray和string之间转换,用在需要处理二进制文件和数据流上

    最近在用python搞串口工具,串口的数据流基本读写都要靠bytearray,而我们从pyqt的串口得到的数据都是string格式,那么我们就必须考虑到如何对这两种数据进行转换了,才能正确的对数据收发 ...

  2. python string module

    String模块中的常量 >>> import string >>> string.digits ' >>> string.letters 'ab ...

  3. Python datatime 格式转换,插入MySQL数据库

    Python datatime 格式转换,插入MySQL数据库 zoerywzhou@163.com http://www.cnblogs.com/swje/ 作者:Zhouwan 2017-11-2 ...

  4. C# Byte[] 转String 无损转换

    C# Byte[] 转String 无损转换 转载请注明出处 http://www.cnblogs.com/Huerye/ /// <summary> /// string 转成byte[ ...

  5. python string

    string比较连接 >>> s1="python string" >>> len(s) 13 >>> s2=" p ...

  6. The internals of Python string interning

    JUNE 28TH, 2014Tweet This article describes how Python string interning works in CPython 2.7.7. A fe ...

  7. Python string objects implementation

    http://www.laurentluce.com/posts/python-string-objects-implementation/ Python string objects impleme ...

  8. C# 之 将string数组转换到int数组并获取最大最小值

    1.string 数组转换到 int 数组 " }; int[] output = Array.ConvertAll<string, int>(input, delegate(s ...

  9. 转:char*, char[] ,CString, string的转换

    转:char*, char[] ,CString, string的转换 (一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准 ...

随机推荐

  1. <Using ZooKeeper><Deploy & Use>

    安装与部署 配置过程相当简单.集群模式部署: wget http://www-us.apache.org/dist/zookeeper/stable/zookeeper-3.4.10.tar.gz t ...

  2. L322

    As a nutritionist helping people shed pounds, I often recommend incorporating portion-controlled des ...

  3. vue中七牛插件使用

    <template> <div id="cxUpload" class="cx-upload"> <button id=" ...

  4. Beta阶段冲刺---Day3

    一.Daily Scrum Meeting照片 二.今天冲刺情况反馈 昨天已完成的工作: (1)数字以扑克牌的形式给出 (2)答案的乘除符号与游戏中的符号保持一致. 今天计划完成的工作 (1)闯关模式 ...

  5. kbmMW SmartService控制返回类型

  6. 2019-03-14-day010-函数进阶

    昨日回顾 1.函数: 函数的定义 函数的参数 位置参数 关键字参数 混合参数 形参的位置上(默认参数) 实参的位置上(关键字参数) 位置参数 > 默认参数 三元运算符: c = a if a&g ...

  7. HDU 6066 17多校3 RXD's date(超水题)

    Problem Description As we all know that RXD is a life winner, therefore he always goes out, dating w ...

  8. http协议与websocket协议(转)

    一.WebSocket是HTML5中的协议,支持持久连接:而Http协议不支持持久连接. 首先HTMl5指的是一系列新的API,或者说新规范,新技术.WebSocket是HTML5中新协议.新API. ...

  9. Git忽略文件权限

    有时 git diff 执行显示文件内容没变化,但是git status却是一片姨妈红: 原因是文件的权限,被chmod变化了,这种变化也被 diff 识别出来了,git diff某个文件类似如下: ...

  10. C语言-第一周作业

    要求1: 请在你的PC或者移动设备上安装词典,该词典工具能对计算机术语进行翻译.需要在你的博客中给出安装软件的截图,请确保阅读者能从截图判断出你的被安装的设备是PC或者移动设备.(5经验值) 要求2: ...