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开发的实用计算器。分享给大家供大家参考,具体如下: 实现功能:图形界面PyQt,输入框,+,—,*,/ ;乘方 ,开方 ,取余,清零。 1. Python代码:...

Python中除法使用的注意事项

本文实例讲解了Python中除法使用的注意事项,是非常重要的技巧,对于Python程序设计来说有很好的借鉴价值。具体分析如下: 现来看如下示例: def avg(first, *re...

如何修复使用 Python ORM 工具 SQLAlchemy 时的常见陷阱

在使用 SQLAlchemy 时,那些看似很小的选择可能对这种对象关系映射工具包的性能产生重要影响。 对象关系映射Object-relational mapping(ORM)使应用程序开...

Python原始字符串与Unicode字符串操作符用法实例分析

Python原始字符串与Unicode字符串操作符用法实例分析

本文实例讲述了Python原始字符串与Unicode字符串操作符用法。分享给大家供大家参考,具体如下: #coding=utf8 ''''' 在原始字符串里,所有的字符串都是直接按照...

快速了解Python相对导入

1、绝对导入和相对导入 绝对导入:按照sys.path顺序搜索,先主目录(sys.path中第一项''),然后PYTHONPATH环境变量、标准库路径、pth指定路径等。 相对导入:...