12. 练习:字符串
练习:修正引言
以下练习中的代码将因为错误地使用引号而导致 SyntaxError。首先请点击“测试答案”查看错误消息。然后解决该问题,将(Henry Ford)的引言正确地赋值给变量 ford_quote
。
Start Quiz:
# TODO: Fix this string!
ford_quote = 'Whether you think you can, or you think you can't--you're right.'
运算符和字符串
SOLUTION:
3415 (并且 tropical_fruit_count 是字符串)练习:编写服务器日志消息
在此编程练习中,你将运用所学的字符串知识编写服务器日志消息。
你将获得用户示例数据、他们的访问时间和访问的网站。你应该使用提供的变量和所学的技能输出如下所示的日志消息(将用户名、网址和时间戳替换为相应变量的值):
Yogesh accessed the site http://petshop.com/pets/reptiles/pythons at 16:20.
当你在编写这段代码时,可以点击 测试答案
按钮,看看结果如何。
Start Quiz:
username = "Kinari"
timestamp = "04:50"
url = "http://petshop.com/pets/mammals/cats"
# TODO: print a log message using the variables above.
# The message should have the same format as this one:
# "Yogesh accessed the site http://petshop.com/pets/reptiles/pythons at 16:20."
练习:len
使用字符串连接和 len
函数计算某些电影明星的实际完整姓名的长度。将该长度存储在变量 name_length
中。注意,姓名不同部分之间有空格!
Start Quiz:
given_name = "William"
middle_names = "Bradley"
family_name = "Pitt"
name_length = #todo: calculate how long this name is
# Now we check to make sure that the name fits within the driving license character limit
# Nothing you need to do here
driving_license_character_limit = 28
print(name_length <= driving_license_character_limit)
len
和整数