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)