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. Oracle审计策略例子

    首先确保Oracle初始化参数audit_trail值为DB或OS,通过“show parameter audit_trail:”查看. 1 语句审计 audit table by test by a ...

  2. TensorFlow函数:tf.zeros_like

    tf.zeros_like函数 tf.zeros_like( tensor, dtype=None, name=None, optimize=True ) 定义在:tensorflow/python/ ...

  3. day 46 前端基础 基本框架

    注意一点 使用绝对路径的时候 在pxm里 打开显示不了图片 可以直接找到那个实际的网页去打开 还可能是图片的格式尽量用jpg一 详细解释 <!DOCTYPE html>声明为HTML5文档 ...

  4. Delphi直接实现分享图片功能

    procedure TCustomCameraViewDoc.ShareTextClick(Sender: TObject); var FSharingService: IFMXShareSheetA ...

  5. Python 黏包及黏包解决方案

    粘包现象 说粘包之前,我们先说两个内容,1.缓冲区.2.windows下cmd窗口调用系统指令 1 缓冲区(下面粘包现象的图里面还有关于缓冲区的解释) 每个 socket 被创建后,都会分配两个缓冲区 ...

  6. [转]JavaScript与元素间的抛物线轨迹运动

    在张鑫旭的博客看到这个抛物线的小动画,觉得很感兴趣,转载一下方便研究~ 原文地址:http://www.zhangxinxu.com/wordpress/?p=3855 在页面上添加元素的位移动画,除 ...

  7. wx小程序修改swiper 点的样式

    <swiper class="swiper-box" indicator-dots="{{ indicatordots }}" autoplay=&quo ...

  8. Maven下用MyBatis Generator生成文件

    使用Maven命令用MyBatis Generator生成MyBatis的文件步骤如下: 1.在mop文件内添加plugin <build> <finalName>KenShr ...

  9. 【转载】 火爆的996.ICU项目正在酝酿开源许可证 禁止996公司使用

    原文地址: https://www.cnbeta.com/articles/tech/832449.htm ---------------------------------------------- ...

  10. 【leetcode】69-Sqrt(x)

    problem Sqrt(x) code class Solution { public: int mySqrt(int x) {// x/b=b long long res = x;// while ...