第六章 课后习题

1、打印字符串"Camuse"中的所有字符。

x = "Camus"
for i in range(len(x)):
    print(x[i])         #输出:C a m u s

2、编写程序,从用户处获取两个字符串,将其插入字符串"Yesterday I wrote a [用户输入1]. I sent it to [用户输入2]!"中,并打印新字符串。

n = input("type a str:")
m = input("type another str:")
x = "Yesterday I wrote a {}. I sent it in to {}!".format(n,m)
print(x)

3、想办法使得"aldous "的第一个字符大写。

print("aldous".capitalize())

4、对字符串 "Where now? who now? when now?" 调用一个方法,返回如下的列表 ["Where now?", "Who now?", "When now?"]。

  不会。征集答案。

5、对列表 ["The","fox","jumped","over","the","fence","."] 进行处理,将其变成一个语法正确的字符串。每个单词以空格符分隔,但是单词fence和句号之间不能由空格符。

x_1 = ["The","fox","jumped","over","the","fence","."]
x_2 = " ".join(x_1)  #'The fox jumped over the fence .'
x_3 = x_2[0:-2]      #'The fox jumped over the fence'
x_4 = "."
print(x_3+x_4)      #  The fox jumped over the fence.

6、将字符串"A  screaming comes across the sky."中所有的"s"字符替换为美元符号。

x = "A screaming comes across the sky".replace("s","$")
print(x)

7、找到字符串"Hemingway"中字符"m"所在的第一个索引

print("Hemingway".index("m"))

8、先后使用字符串拼接和乘法,创建字符串"three three three"。

x_1 = "three "*3
print(x_1)

x_2 = "three "+"three "+"three"
print(x_2)

9、对字符串 "It was bright cold day in April, and the clocks were striking thirteen." 进行切片,只保留逗号之前的字符。

x = "It was bright cold day in April, and the clocks were striking thirteen."
print(x[0:x.index(',')])

 

上一篇:澳洲篇:墨尔本工业厂商优化3个月,Google地图排第一,网站关键词进首页!


下一篇:POJ 1821 Fence