Python


Pythonista's Guide to All Problems in the Galaxy


List和String在資料儲存策略上的不同

String特性及介紹
name="username"
           01234567
print name[2]   >> e
print name[-2]  >> m
print name[:2]   >> use
print name[1:5]  >> sern
print name[4:]   >>name
print name[:]    >> username
print name[2].upper()   >> E

pythagoras = "There is geometry in the humming of the strings, there is music in the spacing of the spheres."
print pythagoras.find("string")  >> 40
print pythagoras[40:]  >> string, there is music in the spacing of the spheres.
print pythagoras.find("algebra")  >> -1(means string is not found)


當S為空字串時第二個選項會出現-1

s.find('')
Remember the definition of find says it will give us the first occurrence where that string appears. Well the empty string appears in s.We don't actually see if, but it's there. We found the empty string. There's no string there no matter what s is. We can find the empty string in it, because we don't need any string to find the empty string. So the result of s. find passing an empty string, will always be 0, no matter the string s is.


danton = "De l'audace, encore de l'audace, toujours de l'audace."
print danton.find('audace')    >>  5
print danton.find('audace',0)  >>  5
print danton.find('audace',5)  >>  5
print danton.find('audace',6)  >>  25
print danton[6:]               >>  udace, encore de l'audace, toujours de l'audace.

print danton[25:]             >>  audace, toujours de l'audace.
print danton.find('audace',25)  >>  25
print danton.find('audace',26)  >>  47
print danton[47:]               >>  audace.

print danton.find('audace',48)  >>  -1

Which of the following sequences of statements leaves the value of variable x the same as it was before the statements.
Assume that both a and x refer to integer values before this code.

當S[0]時S會出現error
第四個,we have to remember that for the subsequence operator, this does not cause an error.

將字串倒著產生


No comments:

Post a Comment