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. day 67 django 之ORM 增删改查基础

    一 操作基础前提准备 1. 新建django 项目 mysite  子项目app01 ,选择好做路径. 2  .2-1在app01 下面models 中引用 模块 from  django.db im ...

  2. LeetCode--1、26、27、35、53 Array(Easy)

      1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to ...

  3. Eclipse_设置_01_自动提示

  4. vue mapbox 地图 demo

    执行以下命令: npm install --save mapbox-gl// cnpm install --save mapbox-gl <template> <div style= ...

  5. each的break

    $.each var arr = [1, 2, 'test', 3, 4, 5, 6] // break $.each(arr, function(index, value) { if (value ...

  6. 安卓与Unity交互之-Android Studio创建Module库模块教程

    安卓开发工具创建Module库 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...

  7. CodeForces - 710F:String Set Queries (二进制分组 处理 在线AC自动机)

    ou should process m queries over a set D of strings. Each query is one of three kinds: Add a string ...

  8. {"errcode":48001,"errmsg":"api unauthorized}

    微信公众号基础知识说明 网页授权获取微信用户信息:两种 scope 域 https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&am ...

  9. rest-framework之频率控制

    rest-framework之频率控制 本文目录 一 频率简介 二 自定义频率类,自定义频率规则 三 内置频率类及局部使用 四 内置频率类及全局使用 五 源码分析 回到目录 一 频率简介 为了控制用户 ...

  10. C++学习(二十八)(C语言部分)之 文件操作

    复习:#define 定义一个宏#include 文件包含#if 条件防止头文件重复包含定义一个宏 判断宏是否定义 判断头文件是否包含#define _STDIO_H_#include<stdi ...