组合数据类型
不同的数据类型有不同的性质。例如,字符串和浮点数不能直接做数学运算。若要将变量 x 转换为整数,可以使用命令 int(x)。类似地,若要将变量 y 转换为字符串,可以使用命令 str(y)。
现在请您通过更改一些数据类型来完成并打印一句话。
变量 company_1、year_1 和 revenue_1 已在您的工作区中提供。
本练习是课程的一部分
Python 金融入门
练习说明
- 将变量
year_1和revenue_1的数据类型转换为字符串。 - 使用这些新变量创建并打印以下句子:"The revenue of Apple in 2017 was $229.23 billion."
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Update data types
year_1_str = ____(year_1)
revenue_1_str = ____
# Create a complete sentence combining only the string data types
sentence = 'The revenue of ' + company_1 + ' in ' + ____ + ' was $' + ____ + ' billion.'
# Print sentence
print(sentence)