查找中文个数:
def str_count(str):
zh = 0
import string
for s in str:
if s.isalpha():
zh += 1
print(zh)
text= input()
str_count(text)
语句去掉首位空格,连续重复空格并改为换行,顶行增改:
songs=input()
title=input()
songs=songs.strip()
t=''
for i in songs:
if (t=='') or (i !=t[len(t)-1]) or (i != ' '):
t+=i
songs = t.replace(' ', '\n')
songs=title+'\n'+songs
print(songs)
找出字符串中出现最多的一个字符,并输出该字出现次数
songs=input()
d={}
songs=songs.replace(' ','')
for i in songs:
if i in d.keys():
d[i]+=1
else:
d[i]=1
m=max(d,key=d.get)
print(m,d[m])
实现文本居中对齐:
songs=input()
t=''
songs=songs.strip()
for i in songs:
if (t=='') or (i !=t[len(t)-1]) or (i != ' '):
t+=i
a=t.split(' ',t.count(' '))
c=[]
for s in a:
s1=len(s)
c.append(s1)
s2=max(c)
for s in a:
num=int((s2-len(s))/2)
s3=' '*num+s
print(s3)