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 判断linux进程,并杀死进程的实现方法

如下所示: ''' @author: Jacobpc ''' import os import sys import subprocess def get_process_id(...

在django模板中实现超链接配置

django中的超链接,在template中可以用{% url 'app_name:url_name' param%} 其中app_name在工程urls中配置的namespace取值,...

Python实现返回数组中第i小元素的方法示例

Python实现返回数组中第i小元素的方法示例

本文实例讲述了Python实现返回数组中第i小元素的方法。分享给大家供大家参考,具体如下: #! /usr/bin/env python #coding=utf-8 #期望为线性时间...

python面向对象入门教程之从代码复用开始(一)

前言 本文从代码复用的角度一步一步演示如何从python普通代码进化到面向对象,并通过代码去解释一些面向对象的理论。所以,本文前面的内容都是非面向对象的语法实现方式,只有在最结尾才给出了...

Python for循环及基础用法详解

Python for循环及基础用法详解

Python 中的循环语句有 2 种,分别是 while 循环和 for 循环,前面章节已经对 while 做了详细的讲解,本节给大家介绍 for 循环,它常用于遍历字符串、列表、元组、...