python(3)——字符串简单操作

1.输入一行字符,编程统计其中有多少个单词。假设单词之间以空格隔开。

#1
s=(input("输入字符串:"))
count=0
for i in s:
   if (ord(i)>=97 and ord(i)<=122) or(ord(i)>=65 and ord(i)<=90):
       count=count+1
print(count)
#2
s=input("请输入字符串:")
num,t,space,d=0,0,0,0
for i in s:
    if i.isdigit():
            num=num+1
    elif i.isalpha():
            t=t+1
    elif  i==' ':
            space=space+1
    else :
            d=d+1
print(num,t,space,d)

2.请输入星期几的第1个字母,用来判断是星期几,如果第1个字母一样,则继续判断第2个字母,以此类推。

a=input("请输入字母:")
if a[0]=='m':
    print("星期一")
elif a[0]=='f':
    print("星期五")
elif a[0]=='w':
    print("星期三")
elif a[0]=='t':
    if a[1]=='u':
        print("星期二")
    elif a[1]=='h':
        print("星期四")
elif a[0]=='s':
    if a[1]=='a':
        print("星期六")
    elif a[1]=='u':
        print("星期日")

3.编写一个程序,计算字符串中子串出现的次数。

string=input("请输入字符串:")
count=0
sub=input("请输入子串:")
cot=string.count(sub)
print('出现了%d次' %cot)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值