python字符串函数

split()函数:

string = “www.shenhuxi.com.cn”

1.以’.’为分隔符

print(string.split(‘.’)) ==>[”www’,’shenhuxi’,’com’,’cn’]

2.分割两次

print(string.split(‘.’,2)) ===>[‘www’,’shenhuxi’,’com.cn’]

response是requests的响应

response.url是返回爬取的页面的链接,

filename = response.url.split(“/”)[-2] 分离字符串
3.分割两次

print(string.split(‘.’,2)[1]) =====>分割两次,并且取序列为1的项=====>shenhuxi

二 分离文件名和路径

import os

print(os.path.split(‘/dodo/soft/python/’))

(‘/dodo/soft/python/’,”)

print(os.path.split(‘/dodo/soft/python’))

(‘/dodo/soft’,’python’)

join


list2 = [‘ming’,’tian’,’hui’,’geng’,’hao’]

li = ”.join(list2)
print(li)
mingtianhuigenghao

li = ‘:’join(list2)

ming:tian:hui:geng:hao:

import os
lis = os.path.join(‘/ming/’,’tian/hui/’,’genghao/’)
‘/mingtianhuigenghao’

你可能感兴趣的:(python字符串函数)