趣學PYTHON第3章 字串

2021-08-14 20:47:36 字數 1139 閱讀 1935

字串的使用,可以是英文單引號,雙引號,都可以輸出

1.但是當字串超過了一行,就要使用三個單引號了。否則會報錯。

三個單引號可以解決這個問題,eol while scanning string literal

2.引號用來標註字串,但是如果輸出的字串也包含引號,怎麼辦。

silly_string = 'he said:'aren't you ?'

print(silly_string)

此時又報錯了。

這次是invalid syntax

錯誤的地方高亮了,如果用三個引號的話,可以在字串的任意位置放單引號和雙引號而不會引起錯誤

silly_string = '''he said:'aren't you ?' '''

print(silly_string)

3.當然,除了這個方法,還可以使用轉義字元。\'.\"

就是在單引號和雙引號前面加\

silly_string = 'he said:\"aren\'t you ?\" '

print(silly_string)

同樣得到正確的結果。

趣學python第3章字串2把值插入到某位置

趣學python第3章字串2把值插入到某位置 1.用 s把值嵌入到字串裡 mysorce 100 message i scored s point.print message mysorce restart c python27 lianxi 31.py i scored 100 point.對於同...

趣學python第7章函式

趣學python第7章函式 函式的組成部分三個,函式名,引數,函式體 def testfunc name print hello,s name testfunc wang 列印出hello,wang restart c python27 lianxi 2018123.py hello,wang de...

趣學python第7章使用模組

趣學python第7章使用模組 1.time模組 import time 引入時間模組 print time.time restart c python27 lianxi 2018123.py 1517052225.95 列印出一串數字 更改列印函式,print time.asctime sat j...