Python中join和split用法实例

yipeiwu_com6年前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.

相关文章

python地震数据可视化详解

python地震数据可视化详解

本文实例为大家分享了python地震数据可视化的具体代码,供大家参考,具体内容如下 参考源码:seisplot 准备工作: 在windows10下安装python3.7,下载参考源码到本...

使用Python进行QQ批量登录的实例代码

具体代码如下所示: #coding=utf-8 __author__ = 'Eagle' import os import time import win32gui import w...

pandas DataFrame索引行列的实现

python版本: 3.6 pandas版本: 0.23.4 行索引 索引行有三种方法,分别是 loc iloc ix import pandas as pd impo...

python代理工具mitmproxy使用指南

python代理工具mitmproxy使用指南

前言 mitmproxy 是 man-in-the-middle proxy 的简称,译为中间人代理工具,可以用来拦截、修改、保存 HTTP/HTTPS 请求。以命令行终端形式呈现,操作...

Python time模块详解(常用函数实例讲解,非常好)

Python time模块详解(常用函数实例讲解,非常好)

在开始之前,首先要说明这几点: 1.在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素。由于Python的ti...