python拆分字符串(.split),以及连接字符串(.join)。
2018-03-26 21:52
716 查看
1,
例子如下:
2,.join。把字符串用设定的连接符连接起来。例子如下:
str.split官方文档如下:
str.split(sep=None, maxsplit=-1) Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made). If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns ['']
seq是分隔符,如
'_',
'.'等。maxsplit是分隔次数,如为1,就从第一个出现分隔符的地方分隔一次,则字符串就变成两串了。
例子如下:
>>> a = 'x_y_z_a_b_c' >>> a.split(sep='_', maxsplit=1) # 拆一次,分隔符为_ ['x', 'y_z_a_b_c'] >>> a.split(sep='_', maxsplit=2) # 拆两次 ['x', 'y', 'z_a_b_c'] >>> a.split(sep='_', maxsplit=-1) # 全拆 ['x', 'y', 'z', 'a', 'b', 'c']
2,.join。把字符串用设定的连接符连接起来。例子如下:
>>> a = 'x_y_z_a_b_c' >>> b = a.split(sep='_', maxsplit=-1)[0:-1] # 全拆之后,取第一至倒数第二的str ['x', 'y', 'z', 'a', 'b'] >>> '_'.join(b) # 用.把str连接起来。 'x.y.z.a.b' >>> ''.join(b) # 用空白连接起来 'xyzab'
相关文章推荐
- Python的字符串连接join和拆分split
- Python的字符串和拆分方法join、split
- Python_连接List与分割字符串_join&split
- python中字符串拆分与合并(split()和join())
- python字符串处理 join split replace的使用方法
- python字符串split()、join()、strip()函数的总结
- 演示了Python中的字符串通过split分割后的使用和把分割后的字符串连接起来
- python join连接字符串Demo
- 0714学习笔记list常用操作索引(index)和切片(slice) 字符串的方法分割split和连接join
- python 字符串 的 join 方法 与 split 方法
- python基础(一)--join和os.path.join函数的用法以及字符串格式化
- Python连接字符串,join出现问题:python 'list' object has no attribute
- Python 入门:字符串连接及join用法
- Python split() 函数 拆分字符串 将字符串转化为列
- C#中用 Join方法和Split方法连接和分割字符串
- 列表连接成字符串(join)(python)
- python 中字符串连接+、join、%替换三种效率问题
- [Python][小知识][NO.2] Python 字符串跨行连接,或拆分为多行显示
- python 4-1 如何拆分含有多种分隔符的字符串re.split()/str.split()
- python--uuid--字符串split--join(列表)