9.19Python

判断是否包含大写字母

x=input("请输入字符串:")
for i in x:
    if 'A' <= i <= 'Z':
        print(True)
        break
    elif i == x[len(x)-1]:
        print(False)

输出字符串里s的位置和数量,替换

string = 'wuhan institute of design and sciences'
print(string.count('s'))
for i in range(0, len(string)):
    n = string.find('s', i, i + 1)
    if n > 0:
        print(n)
print(string.replace(" ", "*"))

字符串的分割和拼接

string = 'wuhan institute of design and sciences'
x = '*'
print(string.split(' '))
print(x.join(string))

英文格式(删除乱码,多余空格,统一大小写)

#string = 'wuhan >>>institute of    design? and!      sciences***/'
# 去乱码
string=input()
for i in string:
    if i != ' ':
        if not i.isalpha():
            string = string.replace(i, '')
#print(string)   #wuhan institute of    design and      sciences
# 去空格
lst = string.split(' ')
temp = []
for item in lst:
    if not item.isalpha():
        temp.append(item)
for item in temp:
    lst.remove(item)
#print(lst)  #['wuhan', 'institute', 'of', 'design', 'and', 'sciences']
#开头
for item in range(0, len(lst)):     # 开头大写
    lst[item] = lst[item].lower()   #统一格式
    lst[item] = lst[item].title()   #开头大写
list_l = ['for', 'of', 'and', 'so']
#print(lst)  #['Wuhan', 'Institute', 'Of', 'Design', 'And', 'Sciences']
for item in list_l:     #连词小写
    for i in range(0, len(lst)):
        if item.lower() == lst[i].lower():  #统一成小写判断
            lst[i] = lst[i].lower()
#print(lst)     #['Wuhan', 'Institute', 'of', 'Design', 'and', 'Sciences']
string = ' '.join(lst)
print(string)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值