码迷,mamicode.com
首页 > 其他好文 > 详细

练1-字符串与正则

时间:2021-03-15 10:59:49      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:test   提示   war   and   位置   eve   程序   ash   失败   

子串位置

# 打印每个o出现的位置
str1 = "hellopythonhelloworld"
str2 = hellopythonhelloworld
str3 = hellopythonhelloworl
index = 0
for i in str1:
    if i == o:
        print(index, end=\t)
    index += 1

# 枚举实现
str1 = hellopythonhelloworl
for i, j in enumerate(list(str1)):
    if j == o:
        print(i)

条件退出

# # 实现用户登录校验功能
# # 要求:分别提示用户输入用户名和密码,当用户名为seven,密码为123或用户名为alex密码为456时,显示登陆成功,并结束程序;否则显示登陆失败并提示用户重新输入用户名和密码.
# # PS:用户只有3次登录校验的机会,第3次登录失败则结束程序。
#
names = [alex, seven]
passwords = [123, 456]
count = 1
while True:
    name = input(your name:)
    password = input(your password:)
    if name in names and password in passwords:
        print(wellcome log in)
        break
    else:
        print(fail to log please try again)
        count += 1
        if count > 3:
            break

统计大小写

# 编写函数,实现接收一个字符串,分别统计大写字母、小写字母、数字字符的个数。
str_a = "Java324javaajlsdf56sdfja54@vajav345aa234ddsfa987dsjkp132132jp324"
count_number = 0
count_lower = 0
count_upper = 0
for i in range(0, len(str_a)):
    if str_a[i].isdigit():
        count_number += 1
    if str_a[i].islower():
        count_lower += 1
    if str_a[i].isupper():
        count_upper += 1
print(数字有%d个 % (count_number))
print(小写字母有%d个 % (count_lower))
print(大写字母有%d个 % (count_upper))

定位全部索引

import re

li = [https://cashiertest.senguo.me/api/warehouse/createpurchaseorder:{"eta":"2020-11-07","purchase_order_goods_list":[{"goods_id":31345,"goods_name":"goods_01","supplier_id":768,"supplier_name":"默认供货商","amount":3,"price":0,"subtotal":0,"remarks":"","purchase_unit_ratio":1,"purchase_unit_id":2}],"confirm":true}, https://cashiertest.senguo.me/api/warehouse/getgoodslastpurchase:{"goods_id":31345}, https://cashiertest.senguo.me/api/warehouse/getshopsupplierdetail:{"supplier_id":768,"page":1,"limit":20}, https://cashiertest.senguo.me/api/warehouse/editsupplier:{"supplier_id":768,"name":"绝对不要删","phone":"213213213"}, https://cashiertest.senguo.me/api/warehouse/createsupplier:{"name":"supply_01","phone":"23424234"}]

ex = https

for match in re.finditer(ex, str(li)):
    print("match found from {} to {}".format(match.start(), match.end()))

 

练1-字符串与正则

标签:test   提示   war   and   位置   eve   程序   ash   失败   

原文地址:https://www.cnblogs.com/teark/p/14527727.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!