jeapedu 37 字符串函数find练习

链接: https://pan.baidu.com/s/1kVW3vG7 密码: 7qcn

# 1 delete black space first and last
# idem
# 2 judge username is ok
# isalpha() isdigit() isalnum() isspace()
# 3 s = "http://www.baidu.com rul http://www.sohu.com rli http://www.ifeng.com heh"
# use str.find to find sub string "http~com" 
# http => posh
# com  => posc

# 2 judge username is ok
s = "uakd"
i = 0
count = True
while i < len(s):
    if not s[0].isalpha():
        count = False
        break
    if s[i].isspace():
        count = False
        break
    i += 1
print(count)

# 3 s = "http://www.baidu.com rul http://www.sohu.com rli http://www.ifeng.com heh"
# use str.find to find sub string "http~com" 
# http => posh
# com  => posc
s = "http://www.baidu.com rul http://www.sohu.com rli http://www.ifeng.com heh"
head = "http://"
tai = ".com"
posh = 0
post = 0
while s.find(head, posh) != -1:
    posh = s.find(head, posh)
    post = s.find(tail, post)
    print(i, posh, post)
    print(s[posh : post + len(tail)])
    posh += len(head)
    post += len(tail)

s = "http://www.jeapedu.com http://chinaswift.com http://www.chinagame.com http://www.163.com"
h = "http"
c = ".com"
posh = -len(h)
posc = -len(c)
i = 0
while i < s.count(h):
    posh = s.find(h, posc + len(c))
    posc = s.find(c, posh + len(h))
    print(i, posh, posc, s[posh : posc + len(c)])
    i += 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值