Python中join和split用法实例

yipeiwu_com5年前Python基础

join用来连接字符串,split恰好相反,拆分字符串的。
不用多解释,看完代码,其意自现了。

复制代码 代码如下:

>>>li = ['my','name','is','bob']
>>>' '.join(li)
'my name is bob'
>>>s = '_'.join(li)
>>>s
'my_name_is_bob'
>>>s.split('_')
['my', 'name', 'is', 'bob']

其join和split的英文版解释如下:

join(...)
S.join(sequence) -> string

Return a string which is the concatenation of the strings in the
sequence.  The separator between elements is S.

split(...)
S.split([sep [,maxsplit]]) -> list of strings

Return a list of the words in the string S, using sep as the
delimiter string.  If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are removed
from the result.

相关文章

解决Django中调用keras的模型出现的问题

笔者小白在用Django写一个表格单据图片的识别应用的时候,遇到了调用基于Tensorflow的keras模型出错的问题。 出现的错误信息类似于以下: ValueError: Ten...

python 文件的基本操作 菜中菜功能的实例代码

python  文件的基本操作 菜中菜 文件操作 ​ open():打开 ​ file:文件的位置(路径) ​ mode:操作文件模式 &#...

在Pycharm中项目解释器与环境变量的设置方法

1.官网下载Pycharm community版如pycharm-community-2017.3.1.tar.gz。 2. #解压tar.gz tar xfz pycharm-*.ta...

使用Eclipse如何开发python脚本

使用Eclipse如何开发python脚本

本文为大家分享了Eclipse开发python脚本的具体方法,供大家参考,具体内容如下 一、安装python 1.访问网址,可以看到如下图所示界面 2.点击上图的"Download",...

pip安装python库的方法总结

使用pip安装python库的几种方式 1、使用pip在线安装 1.1 安装单个package 格式如下: pip install SomePackage 示例如下: 比如:pip...