jeapedu 19 字符串算术运算习题答案

本文通过多个实例介绍了Python中字符串处理的方法,包括序列化输出、字符计数及位置查找等,并展示了如何利用循环和条件判断实现特定数字序列的筛选。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接: https://pan.baidu.com/s/1eUlMJSI 密码: dk43

# 1 print jeapedu000~jeapedu100
# 2 count "c" in "aacacaac" * 100 count and pos
# 3 random.randint(100000,999999) 
#   1) if 4 and 7 in it, otherwise drop the num
#   2) when not 4 and 7 in it, whether 6 and 8 in it

# 1 print jeapedu000~jeapedu100
#jeapedu000
#jeapedu001
#jeapedu009
#...
#jeapedu010
#jeapedu011
#jeapedu099
#...
#jeapedu100
# while
i = 0
while i <= 100:
    if i < 10:
        print("jeapedu00" + str(i))
    elif i < 100:
        print("jeapedu0" + str(i))
    else:
        print("jeapedu" + str(i))
    i +=1
# str.zfill(width) str method
i = 0
while i <= 100:
    s = str(i)
    s = s.zfill(3)
    print("jeapedu" + s)
    #print("jeapedu" + str(i).zfill(3))
    i += 1
# num formatting
i = 0
while i <= 100:
    s = "%03d" %i
    print("jeapedu" + s)
    #print("jeapedu" + "%03d" %i)
    i += 1
# num format function
i = 0
while i <= 100:
    s = "{0:03d}".format(i)
    print("jeapedu" + s)
    i += 1

# 2 count "c" in "aacacaac" * 100 count and pos
s = "aacacaac" * 100
i = 0
count = 0
while i < len(s):
    if s[i] == "c":
        print("found", s[i], i)
        count += 1
    i += 1
print(count)

# 3 random.randint(100000,999999) 
#   1) if 4 and 7 in it, otherwise drop the num
#   2) when not 4 and 7 in it, whether 6 and 8 in it
import random
i = 0
while i < 20:
    x = random.randint(100000,999999)
    s = str(x)
    j = 0
    if "4" in s or "7" in s:
        print(s, end=' ')
        while j < len(s):
            if s[j] == "4" or s[j] == "7":
                print(s[j], j, end=' ')
            j += 1
    elif "6" in s or "8" in s:
        print(s, end=' ')
        while j < len(s):
            if s[j] == "6" or s[j] == "8":
                print(s[j], j, end=' ')
            j += 1

    print("")
    i += 1

import random
x = random.randint(100000, 999999)
print(x + 100, type(x), str(x), type(str(x)))
s = str(x)
i = 1
while i < 20:
    x = random.randint(100000, 999999)
    s = str(x)
    #if "4" in s:
        #print(i, "has 4 in", s)
    if "4" in s and "7" not in s:
        print(i, "has 4 in", s)
    else:
        print(i, s)
    i += 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值