Python基础知识2——join 和 split 的使用方法

Python join用来连接字符串,split用来拆分字符串。

1、只针对字符串进行处理。split:拆分字符串、join连接字符串
2、string.join(sep):  以string作为分割符,将sep中所有的元素(字符串表示)合并成一个新的字符串
3、string.split(str=' ',num=string.count(str)):  以str为分隔,符切片string,如果num有指定值,则仅分隔num个子字符串。

Python join方法

a = 'abcd'
print '+'.join(a)   
print '-'.join(['a','b','c'])
print '.'.join({'a':1,'b':2,'c':3,'d':4})

--------output---------

a+b+c+d

a-b-c

a.c.b.d

Python split方法

s ='a b c'
print s.split(' ')
st ='hello world'
print st.split('o')
print st.split('o',1)

--------output---------
['a', 'b', 'c']
['hell', ' w', 'rld']
['hell', ' world']




猜你喜欢

转载自blog.csdn.net/subkiller/article/details/22615309