String 字元/Create 建立
- Python 文字的型別稱為 str ,是 string 的簡寫
- 使用單引號或雙引號將值包括起來
- 不論是在引號中放置數值、文字或者布林,都會以文字型別儲存。
asset_tony_stark = "12.4 billion"
print(type(asset_tony_stark))
## <class 'str'>
asset_tony_stark = "12400000000"
print(type(asset_tony_stark))
## <class 'str'>
tony_stark_is_rich = "True"
print(type(tony_stark_is_rich))
## <class 'str'>
- 文字內容中有出現雙引號或單引號的時候,要特別留意
- 兩種解法:
- 在 O’Neal 的單引號前面加上跳脫符號 \
- 以雙引號包括姓名
print("Shaquille O'Neal")
## Shaquille O'Neal
print('Shaquille O\'Neal')
## Shaquille O'Neal
'Shaquille O'Neal'
## SyntaxError: invalid syntax## Error: unexpected symbol in "'Shaquille O'Neal"
"Shaquille O'Neal"
## [1] "Shaquille O'Neal"
'Shaquille O\'Neal'
## [1] "Shaquille O'Neal"
print("Okay. Let's put aside the fact that you \"accidentally\" picked up my grandmother's ring and you \"accidentally\" proposed to Rachel.")
## Okay. Let's put aside the fact that you "accidentally" picked up my grandmother's ring and you "accidentally" proposed to Rachel.
writeLines("Okay. Let's put aside the fact that you \"accidentally\" picked up my grandmother's ring and you \"accidentally\" proposed to Rachel.")
## Okay. Let's put aside the fact that you "accidentally" picked up my grandmother's ring and you "accidentally" proposed to Rachel.
Reference: