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绘制人人网好友关系图示例

代码依赖:networkx matplotlib 复制代码 代码如下: #! /bin/env python# -*- coding: utf-8 -*-import urll...

Python一行代码解决矩阵旋转的问题

今天刷《剑指offer》的时候碰到这样一道题: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10...

对dataframe进行列相加,行相加的实例

实例如下所示: >>> import pandas as pd >>> df = pd.DataFrame({"x":['a','b','c','...

计算机二级python学习教程(1) 教大家如何学习python

计算机二级python学习教程(1) 教大家如何学习python

本来PHP还学艺不精,又报了计算机二级Python的考试,还有一个半月的时间,抓紧买了高教社的这两本书,今天正式开始学习这个语言,虽然没法和世界上最好的语言PHP相提并论,但是也值得一学...

python实现csv格式文件转为asc格式文件的方法

python实现csv格式文件转为asc格式文件的方法

一、背景描述 csv格式文件是一种类似于excel的文件格式 asc格式文件是一种可以用text打开的文本文件 csv转asc本来可以用arcgis顺利完成,但由于csv数据量太大(74...