r’\t’ ⇒字串前面加r代表是以原始字串來表示 \t
使用三重引號來表示跨越數行的字串,如: ''' xxxxxxxxxxxxxx xxxxxxxxxxxxxxx'''
裡面內容不管輸入什麼就是什麼(就算是換行或縮排),如上面的例子就是多了一個\n
‘apple’ in ‘my_apple’ ⇒測試字串內容是否存在 True
text1 = ‘Just’ text1 * 10 ‘JustJustJustJustJustJustJustJustJustJust’
ord(‘A’) ⇒查Ascii碼 65 chr(65) ⇒由Ascii碼轉文字 ‘A’
name = ‘Justin’ name[0] ‘J’ name[1] ‘u’ name[-1] ‘n’ name[-2] ‘i’
name[i:j:k],意思是切出起始索引i與結尾索引j(不包括)之間,每隔k元素的內容 name[::-1] ⇒反轉字串
字串格式化方法: ‘{name} is {age} years old!‘.format(name = ‘Justin’, age = 35)
‘My platform is {0.platform}. PI = {1.pi}.‘.format(sys, math) ‘My platform is win32. PI = 3.14159265359.’ ‘My name is {person[name]}‘.format(person = {‘name’ : ‘Justin’, ‘age’ : 35})//person為一個key-value的map pair
在預留空白時,可以使用>指定靠右對齊,使用>> ‘example:{0:>10.2f}!!‘.format(19.234) ‘example: 19.23!!’
‘example:{0:<10.2f}!!‘.format(19.234) ‘example:19.23 !!’
如果只是要格式化單一數值,則可以使用format()函式。例如:
import math format(math.pi, ‘.2f’) ‘3.14’
對串列使用[:]跟 *,所進行的會是潛層複制
list8 = [[1, 2, 3], [4, 5, 6]] list9 = list8 * 2 list9 [[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]] list9[0][0] = 100 list9 [[100, 2, 3], [4, 5, 6], [100, 2, 3], [4, 5, 6]] list8 [[100, 2, 3], [4, 5, 6]]
如果你想從字串中取出字元,包括在串列中,則可以如下:
list(‘Justin’) [‘J’, ‘u’, ‘s’, ‘t’, ‘i’, ‘n’]