Pythonチラシ

2036 ワード

ここをクリックしてください
継続的な更新
1、文字列
upper()、lower()大文字と小文字の変換
str = "TEST LOWER()"
str1 = "test upper()"
print(str.lower())
print(str1.upper())

#test lower()
#TEST UPPER()

split()
Python split()は、区切り記号を指定する文字列をスライスし、パラメータnumに指定値がある場合はnumのサブ文字列のみを区切る.
#!/usr/bin/python

str = "Line1-abcdef 
Line2-abc
Line4-abcd" print str.split( ) print str.split(' ', 1 ) #['Line1-abcdef', 'Line2-abc', 'Line4-abcd'] #['Line1-abcdef', '
Line2-abc
Line4-abcd']

replace()
replace()メソッドは、文字列のold(古い文字列)をnew(新しい文字列)に置き換え、3番目のパラメータmaxを指定するとmax回を超えません.置換する文字列が見つからない場合は、変更されません.
#!/usr/bin/python

str = "this is string example....wow!!! this is really string"
print str.replace("is", "was")
print str.replace("is", "was", 3)

#thwas was string example....wow!!! thwas was really string
#thwas was string example....wow!!! thwas is really string

2、List
reverse()
reverse()関数は、リスト内の要素を反転させるために使用されます.
#!/usr/bin/python

aList = [123, 'xyz', 'zara', 'abc', 'xyz']

aList.reverse()
print "List : " ,aList

#List: ['xyz', 'abc', 'zara', 'xyz', 123]

insert()、append()
#!/usr/bin/python

aList = [123, 'xyz', 'zara', 'abc']

aList.insert( 3, 2009)
aList.append( 2008)

print "Final List : %s" % aList

#Final List : [123, 'xyz', 'zara', 2009, 'abc',2008]

3、辞書
update()
Python辞書(Dictionary)update()関数は、辞書dict 2のキー/値ペアをdictに更新します.
#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7}
dict2 = {'Sex': 'female' }

dict.update(dict2)
print "Value : %s" %  dict

#Value : {'Age': 7, 'Name': 'Zara', 'Sex': 'female'}