牛客网专项练习30天Pytnon篇第11天

发布于:2022-11-28 ⋅ 阅读:(175) ⋅ 点赞:(0)

1.在Python3中,字符串的变换结果为:(C)

strs = 'I like python and java'
print(strs.replace('I', 'Your'))
print(strs.replace('a', '*', 2))

A.'Your like python and java','I like python *nd j*v*'

B.'I like python and java','I like python *nd j*v*'

C.'Your like python and java','I like python *nd j*va'

D.'I like python and java','I like python *nd j*va'

解析:

       在Python3中,string.replace(str1, str2,  num=string.count(str1)),把 string 中的 str1 替换成 str2,如果 num 指定,则替换不超过 num 次。因此 strs.replace('I', 'Your') 的结果为:'Your like python and java';strs.replace('a', '*', 2)的结果为:'I like python *nd j*va',只会替换字符串中的前两个 ‘a’ 字符。


2.在Python3中,下列程序返回的结果为:(B)

strs = '123456'
print(strs.find('9'))

A.None 

B.-1 

C.报错 

D.无打印

解析:

       在Python3中,str.find(str, beg=0, end=len(string)) 检测字符串中是否包含子字符串 str ,如果指定 beg(开始)和 end(结束)范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。


3.在Python3中,下列程序结果为:(C)

strs = ' I like python '
one = strs.split(' ')
two = strs.strip()
print(one)

A.['', 'I', 'like', 'python', ''],'I like python     '

B.['I', 'like', 'python'],'I like python'

C.['', 'I', 'like', 'python', ''],'I like python'

D.['I', 'like', 'python'],'I like python '

解析:

       在Python3中split(str)函数表示以 str 为分隔符切片 strs,其中 str = '   ',则分隔后的结果为 ['', 'I', 'like', 'python', '']strip() 函数删除字符串两边的指定字符,括号里写入指定字符,默认为空格,因此结果为 'I like python'。

       注意: strs字符串前后存在空格。


4.在Python3中,关于字符串的运算结果为:(C)

strs = 'abcd'
print(strs.center(10, '*'))

A.'abcd'

B.'*****abcd*****'

C.'***abcd***'

D.'   abcd   '

解析:

       在Python3中,center() 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。默认填充字符为空格。题目中填充长度 width=10, 填充字符为 ‘*’,最终的结果为 '***abcd***'。


5.在Python3中,关于字符串的判断正确的是:(B)

str1 = ''
str2 = ' '
if not str1:
    print(1)
elif not str2:
    print(2)
else:
    print(0)

A.0   

B.1   

C.2   

D.None

解析:

       在Python3中,逻辑运算符 not 表示非,字符串 str1 表示空字符, str2 虽然也表示空格字符,其长度是为1,因此在题目中会判断 str1 为空打印 1。


cd353671049d40d087a4f6c5d2940bea.jpeg

 

 

 


网站公告

今日签到

点亮在社区的每一天
去签到