共計 402 個字符,預計需要花費 2 分鐘才能閱讀完成。
當需要拼接多個字符串時,可以使用加號 +
連接它們,也可以使用 f-string 或者 .join()
方法。以下是更詳細的說明:
-
使用加號
+
連接字符串:str1 = "Hello, " str2 = "World!" result = str1 + str2 print(result) # 輸出:Hello, World!
-
使用 f-string:
name = "Alice" age = 30 message = f"My name is {name} and I am {age} years old." print(message) # 輸出:My name is Alice and I am 30 years old.
-
使用
.join()
方法:words = ["Hello", "World", "!"] result = " ".join(words) print(result) # 輸出:Hello World !
這些方法都可以用來拼接字符串,選擇其中一種取決于個人偏好和具體情況。
丸趣 TV 網 – 提供最優質的資源集合!
正文完