結合不同資料型別
不同的資料型別有不同的特性。例如,字串和浮點數不能直接做數學運算。若要把變數 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)