1. 程式人生 > >Python 中一些代碼的功能2

Python 中一些代碼的功能2

代碼 位置 pen 是不是 python nes fin capital case

  name="i have a beautiful flower"

  print(name.capitalize())#使name中的首字母大寫

  print(name.count("a"))#統計name中a的個數

  print(name.center(30,"-"))#使name中的字符數一共為30個字符,不夠的用-填充

  print(name.endswith("flower"))#判斷字符串是否以flower結尾,可用來判斷郵件地址是否正確等

  print(name.expendtabs(tabsize=30))#擴展name的字符數為30,不夠的用空格填充

  print(name.find("a"))#查找這個字符串的位置

  print(name[name.find("name"):])#切片

  print(name.isalnum()) #僅有阿拉伯數字加阿拉伯字符為true否者為flase

  print(name.isalpha()) #判斷是否為純英文字符

  print(‘1.234‘.isdecimal())#判斷是否為整數,整數位真,小數為假

  print(‘1a‘.isdigit())#判斷是否為整數,可以用來控制輸如的位數字,是數字才執行if語句或者else

  print(‘a和我1A‘.isidentifier())#判斷是不是一個合法的標識符

  print(‘a和 我1A‘.isidentifier())#判斷是不是一個合法的標識符

  print(‘33‘.isnumeric())#判斷是否只有數字,否者為flase

  print(‘My name is ‘.istitle())#判斷是否為標題名,首字母大寫即為真

  print(‘My Name Is ‘.isprintable())#tty file, drive file

  print(‘M ‘.isupper())#判斷是否全是大寫

  print(name.ljust(50,‘*‘))#保證這句話長50,不夠的用*在後面補上

  print(name.rjust(50,‘*‘))#在前補上

  print(‘Abc‘.lower())#大寫變小寫

  print(‘Abc‘.upper())#小寫變大寫

  print(‘\nAbc\n‘.lstrip())#默認去掉左邊的空格和回車

  print(‘\nAbc\n‘.rstrip())#默認去掉右邊的空格和回車

  print(‘\nAbc\n‘.strip())#默認去掉兩頭的空格和回車

  加密: p=str.maketrans("abcdefli",‘[email protected]‘)

       print("zhou xi".translate(p))#加密編碼

  print(‘zhou xi‘.replace(‘l‘,‘o‘,1))#替換,數字表示替換幾個

  print(‘zhou xi‘.rfind(‘i‘))#查找最後面一個值得下標進行反回

  print(‘zhou wo om‘.split(‘o‘))#把字符串按照o分成[‘zh‘, ‘u w‘, ‘ ‘, ‘m‘]

  print(‘1+2+3+4‘.split(‘+‘))#去掉+號變成數組[‘1‘, ‘2‘, ‘3‘, ‘4‘]

  print(‘1+2\n+3+4‘.splitlines())#按換行符分

  print(‘Zhou Xi‘.swapcase())#大小寫互換

  print(‘zhou xi‘.title())#標題大寫

  print(‘zhou xi‘.zfill(50))#50字符,不夠的用0填充

  while True:真的時候持續循環,while flase:假的時候退出循環

  name.append()#附加

Python 中一些代碼的功能2