The Variable
基本介紹Basic Intro
用 hash symbol (井字鍵)加comment
The equal sign means assignment.
Strings
single quote 或 double quote,沒加的話電腦以為是Variable
EOL means end of line.
不能把字串直接加數字
e.g.
print ‘my age is ’ + 9
可能用* 的
e.g.
print ‘!’ * 12
Indexing String
裡面的數字還可以放負數
Indexing String的延伸
用冒號抓出中間的字串
沒寫後面代表到end
沒寫前面代表從頭抓
單寫: 就是全部
Selecting Sub Sequences
有趣的練習測試,找出能代表完整Variable的寫法
在Empty String(字串內什麼都沒有)中,任何位置都是Error,對於範圍卻是空字串
Find strings in strings
找不到就顯示負1
大小寫會影響s
Find empty string always is zero.
有’’和沒有’’意義差很大,一個是從上方定義的變數找,一個是從‘’內的字串找
最後一個是變成,要找的字串比原本定義的多,當然就找不到
Practice with string.find()
print "test"[2:]
會印出 st
print "text".find("") # prints 0
print "text".find("") # prints -1
有趣的思考
print "Example 3: Printing out everything after a certain substring"
my_string = "My favorite color: blue"
color_start_location = my_string.find("color:")
favorite_color = my_string[color_start_location:]
print favorite_color # oops, this line prints out 'color: ' as well...
print favorite_color[7:] # this fixes it!
Finding substrings with parameter of number
number代表從哪裏開始找
Strings觀念考題
But ifsis the''empty string, thens[0]will cause an error because there is no character at position 0.
重點:Subsequence operator (中間那個冒號:) doesn’t cause error
重要:如何在同一字串中找到第二組一樣的字串
有二種解法