Python自學筆記9 字串

2021-09-22 12:59:17 字數 1072 閱讀 8379

1、字串就是字元的串,「hello,word」

>>> print("hello,word")

hello,word

>>> print('hello,word')

hello,word                                  #可以看出單純的字串輸出,單引號和雙引號是一樣的。

2、字串中有單引號或雙引號的,需要用另一種引號區分如。

print(" i'm a coder") 或者print(' "一起學習python" ')

3、轉義符 「 \ 」,如

print('let\'s go!"一起學習吧" ')---->let's go ! "一起學習吧」

其他轉義符

\b 退格   、\n 換行 、\r 回車 、  \t  製表符 、\"  雙引號、\' 單引號 、\\ 反斜槓

4、字串的鏈結

>>> s1="hello," 'word'

>>> print(s1)

hello,word

>>> s1="hello,"

>>> s2='word'

>>> print(s1+s2)

hello,word

5、str()與repr()將內容轉化成字串

因為字串和數值是不能鏈結的,因此需要聯絡輸出時需要將數值型別轉化為字串。

兩個函式作用相同,但輸出結果不同,如print(str(s1+s2))   返回hello,word      而print(repr(s1+s2)) 返回 『hello,word』 區別在於repr是帶引號的字串輸出

6、長字串 就是用print 輸出三個引號(單雙)的內容會以原格式直接輸出如

>>> print('''hello,

...         word

...            tom''')

返回hello,

word

tom7、原始字串 『r』

print(r'c:\python\ide')

返回  c:\python\id   與使用轉義符「\」結果相同   c:\\python\\ide  。

python自學日記4 字串

使用for迴圈遍歷字元 由於好久沒用for迴圈了,有點生疏,竟然寫成了下面 fruit banana len fruit index 0for index len fruit print fruit index index 1file line 3 for index報錯是肯定的了,這是把for迴圈...

9 字串排序

字串排序 time limit 1000 ms memory limit 65536 kb description 輸入3個字串,按字典序從小到大進行排序。input 輸入資料有一行,分別為3個字串,用空格分隔,每個字串長度不超過100。output 輸出排序後的三個字串,用空格分隔。sample ...

Python筆記(二)字串

記憶體位址 字串為不可變型別,原先指向字串的位址是不可改變的 line he line copy line print id line 2607584542648 print id line copy 2607584542648 line she he print id line 260758458...