# 【1-2】輸出 大家應該對 Scratch 不陌生吧? 我們在 Scratch 中會使用「說出」這個積木,讓角色把想說的話呈現在畫面上。   在 Python 中,我們使用 `print()` 輸出內容。 ## 輸出指令-cout C++ 的輸出指令是 `cout`,透過 `<<` 來連接想要輸出的內容。 ```cpp= cout << 1203; ``` ### 輸出結果 ``` 1203 ``` ### Python對照 ```python= print(1203) ``` --- 如果要輸出字元,就要加上 `''`。 ```cpp= cout << 'A'; ``` ### 輸出結果 ``` A ``` ### Python對照 ```python= print('A') ``` --- 如果要輸出字串,就要加上 `""`。 ```cpp= cout << '子柚'; ``` ### 輸出結果 ``` 子柚 ``` ### Python對照 ```python= print("子柚") ``` --- 如果要輸出的內容已經被變數儲存起來了,那就直接輸出變數名稱就可以了。 ```cpp= int a = 10; cout << a; ``` ### 輸出結果 ``` 10 ``` ### Python對照 ```python= a = 10 print(a) ``` --- 如果要輸出多次,又不想打很多 `cout`,則可以使用連續輸出,透過 `<<` 把想輸出的內容串起來。 ```cpp= cout << "水" << "木" << "目"; ``` ### 輸出結果 ``` 水木目 ``` ### Python對照 ```python= print("水" + "木" + "目") ``` --- 若想將輸出的內容分行,可以使用 `endl` 或 `\n`。 ```cpp= cout << "水" << endl << "木" << endl << "目"; ``` ```cpp= cout << "水\n木\n目"; ``` ### 輸出結果 ``` 水 木 目 ``` ### Python對照 ```python= print("水\n木\n目") ``` --- ## 練習題 [U0001-不良行為警告](https://code.dali.tc.edu.tw/problem/U0001) [U0002-脫離哥布林](https://code.dali.tc.edu.tw/problem/U0002) --- 聯絡方式:codecodefunny@gmail.com 最後編修時間:2025/07/15 子柚筆
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up