内容正文:
字符串专题练习
一、选择题
1. 下列Python字符串不合法的是:( )
A.”I love ‘China’” B.’I “love” China’
C.’I love \’China\’’ D.’I love ‘China”
2.有如下程序段:
S=“2022 Hope To Get Out Of The Epidemic”
t=“”
for i in s:
if i<“a” or i>“z”:
t=””
t=i+t
print(t)
该程序运行后,最后显示的内容是( )
A.cimedipE B.cimedip C.cimed D.cime
2. 有如下Python程序段:
a=”Beautiful is better than ugly.”.split()
b=””.join(a)
则下列说法正确的是:( )
A. a[4]的值为“ugly”,b[4]的值为“t”
B. a[4]的值为“ugly.”,b[4]的值为“t”
C. a[-4]的值为“is”,b[-4]的值为“u”
D. a[-4]的值为“Beautiful”,b[-4]的值为“g”
4.有如下python 程序段:
def fun(a,b):
n=min(len(a),len(b))
for i in range(n):
if a[i]>b[i]:
return a
elif a[i]<b[i]:
return b
if len(a)>len(b):
return a
else:
return b
print(fun(“126”,”26”))
则程序执行后,输出的结果为:( )
A.a B.b C.126 D.26
5.有如下python程序段:
a=input()
t,s=0,0
for c in a[::-1]:
if c.isdigit():
t=t*10+int(c)
else:
s+=t
t=0
print(t)
当输入“123ABC456”时,程序输出结果为: ( )
A.321 B.654 C.123 D.975
6.有如下Python程序段:
text=”床前明月光,疑是地上霜。举头望明月,低头思故乡。”
text=text.replace(“。”,“,”)
ans=[s[0] for s in text.split(“,”) if s]
ans=“”.join(ans)
则程序执行后,ans的值为( )
A.“床前明月光” B.“床前明月”
C.“床疑举低” D.”举头望明”
7.有如下python程序段:
text=“It is too old to learn.”
ans=text[:6]+“never”+text[6:]
print(ans)
则程序执行后,输出的结果为( )
A. It is too old to learn
B. It is never too old to learn
C. It is nevertoo old to learn
D. It isnever too old to learn
8. 有如下Python程序段:
for ch in “I have a dream”:
if ch not in”aoeiu”:
continue
print(ch,end=””)
则程序执行后,输出的结果是( )
A.无输出 B.I have a dream
C.I hv drm D.aeaea
9. 有如下python程序段:
s=“Python3.8”
ans=“”
for ch in s:
if ch.isupper():
ans+=ch.lower()
elif ch.islower():
ans+=ch.upper()
else:
ans+=ch
print(ans)
则程序执行后,输出的结果为( )
A.Python B.Python3.8
C.pYTHON D.pYTHON3.8
10. 有如下程序段:
s=””
ch=”GaozhouKG2021”
n=len(ch)
for i in r