類別

可使用type()查看

內建 [ 布爾:bool (Boolen)  字串:str (String)  數字:int (Integer)  小數:float 列表:list  元祖:tuple  字典:dict ]

亦可用class宣告新類別

布爾值  (用於比較、邏輯、成員判定之運算)

以 1=True,  0=False 紀錄於記憶體中

資料判定上,有東西 or 判定為真=True,沒東西( "", [ ], ( ), { }, 0 )or判定為假=False

有邏輯運算 ( and   or   not)

有關係符 ( < (小於)   <= (小於等於)   == (等於)  >= (大於等於)  > (大於)  != or <> (不等於) )

字串 (用於算術、賦值運算)

常用之方法: join  split  strip  find  upper  lower  replace

索引與獲取   test [位置]

片段   test [ 起始 : 終點之前]

長度   len( 字符串 )

大小写转换

str.capitalize()  首字母大寫

str.casefold()  轉為對應之小寫(含符號式小寫)

str.sapcase()

str.lower()  全英文小寫  

str.upper()  全英文大寫  

str.title()  每個字首都轉大寫

def capitalize(self, *args, **kwargs):
def lower(self, *args, **kwargs):
def upper(self, *args, **kwargs):
def casefold(self, *args, **kwargs):
def swapcase(self, *args, **kwargs):
def title()(self, *args, **kwargs):

判定系列

str.islower()  是否為全英文小寫  str.isuuper  是否為全英文大寫

str.isalnum()  是否只為數字or字符(無空白)  str.isalpha()  是否只為字符

str.isdicimal()  是否為十進制的數字  str.isdigit()  是否為數字  str.isnumeric()  是否為數字(含中文數字)

str.isidentifier()  是否為字母、數字、下划線

str.isprintable()  是否有不可見的功能字符(ex: \t, \n 等)

str.isspace()  是否全是空白格

str.istitle()  是否字首都為大寫

def isupper(self, *args, **kwargs):
def islower(self, *args, **kwargs):
def isalnum(self, *args, **kwargs):
def isalpha(self, *args, **kwargs):def isdecimal(self, *args, **kwargs):
def isdigit(self, *args, **kwargs):
def isidentifier(self, *args, **kwargs):
def isnumeric(self, *args, **kwargs):
def isprintable(self, *args, **kwargs):
def isspace(self, *args, **kwargs):
def istitle(self, *args, **kwargs):

字符串格式输出

str.Center(  width[, fillchar]  )  設定寬度並將物件置中,其於以填充物填補,若無標示則默認為space

str.ljust( width[, fillchar]  )  設定寬度並將物件置左,其於以填充物填補,若無標示則默認為space

str.rjust(  width[, fillchar]  )  設定寬度並將物件置右,其於以填充物填補,若無標示則默認為space

str.zfill(  width  )  設定寬度並將物件左側以0塞滿

def center(self, *args, **kwargs):
def ljust(self, *args, **kwargs):
def rjust(self, *args, **kwargs):
def zfill(self, *args, **kwargs):

  

str.format(*args, **kwargs)

def format(self, *args, **kwargs):

# default arguments
print("Hello {}, your balance is {}.".format("Adam", 230.2346)) # positional arguments
print("Hello {0}, your balance is {1}.".format("Adam", 230.2346)) # keyword arguments
print("Hello {name}, your balance is {blc}.".format(name="Adam", blc=230.2346)) # mixed arguments
print("Hello {0}, your balance is {blc}.".format("Adam", blc=230.2346)) #dictionry
print("Hello {0}, your balance is {blc}.".format(**{"Adam", blc=230.2346})

亦可作為數值格式調整

: 号后面带填充的字符,只能是一个字符,不指定则默认是用空格填

^, <, > 分别是居中、左对齐、右对齐(默認),后面带宽度 (ex {:^10d} )

.nf 表示至小數下第n位

正数前显示 +,负数前显示 -;  (空格)表示在正数前加空格 (ex: {:+.2f} )

b、d、o、x 分别是二进制、十进制、八进制、十六进制

# integer numbers with minimum width
print("{:5d}".format(12))
# width doesn't work for numbers longer than padding
print("{:2d}".format(1234))
# padding for float numbers
print("{:8.3f}".format(12.2346))
# integer numbers with minimum width filled with zeros
print("{:05d}".format(12))
# padding for float numbers filled with zeros
print("{:08.3f}".format(12.2346))
12
1234
12.235
00012
0012.235

str.format_map(mapping) mapping 是一个字典对象

def format_map(self, mapping):

point = {'x':4,'y':-5}
print('{x} {y}'.format_map(point))

str.expandtabs(tabsize=8)

用指定的空格替代横向制表符,使得相邻字符串之间的间距保持在指定的空格数以内,可搭配\t斷句、\n換行使用。

def expandtabs(self, *args, **kwargs):

字符串搜索定位

str.count( "目標"[, 起始=none, 結束=none] )  尋找子序列的出現次數,不可為int(數字)  左側依序以0開始,往右隨之遞增,最右側起始為 -1,往左隨之遞減 (end位置數值不包含本身) 

def count(self, sub, start=None, end=None) 

str,endwith( "目標"[, 起始=none, 結束=none] )  是否以目標結尾,不可為int(數字)  左側依序以0開始,往右隨之遞增,最右側起始為 -1,往左隨之遞減 (end位置數值不包含本身)  

str.startwith( "目標"[, 起始=none, 結束=none] )  是否以目標開頭,不可為int(數字)  左側依序以0開始,往右隨之遞增,最右側起始為 -1,往左隨之遞減 (end位置數值不包含本身)  

def endswith(self, suffix, start=None, end=None):
def startswith(self, prefix, start=None, end=None): 

str.find( "目標"[, 起始=none, 結束=none] )  從左側找起,目標為第幾個,不可為int(數字)  左側依序以0開始,往右隨之遞增,最右側起始為 -1,往左隨之遞減 (end位置數值不包含本身,若無發現目標則以-1表示)

str.rfind( "目標"[, 起始=none, 結束=none] )  從右側找起,目標為第幾個,不可為int(數字  左側依序以0開始,往右隨之遞增,最右側起始為 -1,往左隨之遞減 (end位置數值不包含本身,若無發現目標則以-1表示)  

def find(self, sub, start=None, end=None):
def rfind(self, sub, start=None, end=None):  

str.index( "目標"[, 起始=none, 結束=none] )  約等於find,但無發現時程式無法執行

str.rindex( "目標"[, 起始=none, 結束=none] )  約等於rfind,但無發現時程式無法執行

def index(self, sub, start=None, end=None):
def rindex(self, sub, start=None, end=None):

  

字符串搜索与替换

str.replace(old, new[, count])  count默認為全部

def replace(self, *args, **kwargs):

str.strip([chars])  依序從兩端去除指定物內的子集物,直到無法刪除則停止,chars值若為none則去除所有空白格及\t、\n

str.lstrip([chars])   依序從左側去除指定物內的子集物,直到無法刪除則停止,chars值若為none則去除所有空白格及\t、\n

str.rstrip([chars])  依序從右側去除指定物內的子集物,直到無法刪除則停止,chars值若為none則去除所有空白格及\t、\n

def strip(self, *args, **kwargs):
def lstrip(self, *args, **kwargs):
def rstrip(self, *args, **kwargs):

static str.maketrans(x[, y[, z]])  對應表

str.translate(table)  制換

def maketrans(self, *args, **kwargs):
def translate(self, *args, **kwargs):

maketrans可以為直接以(  )指定,但前後對應表需具有相同字元數,亦可用字典帶入參數

a = 'dobi is a dog'
table = str.maketrans('dobi', 'alph')
a.translate(table)
# 'alph hs a alg'
(d=>a o=>l b=>p i =>h) table = str.maketrans('dobi', 'alph', 'o')
a.translate(table)
# 'aph hs a ag'
(第三個參數,表示將原先的o,改對應成none)

字符串的联合与分割

str.partition(sep)  從左側找到第一個符合的目標後,以目標作前後切割,獲得三份資料

str.rpartition(sep)  從右側找到第一個符合的目標後,以目標作前後切割,獲得三份資料

str.split(sep=None, maxsplit=-1)  從左側找到第一個符合的目標後,將目標切除,獲得兩份資料,目標若無指定則默認為全數切分

str.rsplit(sep=None, maxsplit=-1)  從右側找到第一個符合的目標後,將目標切除,獲得兩份資料,目標若無指定則默認為全數切分

str.splitlines([keepends])  從左側起找行界符,如果keepend為True則保留,若無指定則默認將行界符切除

def partition(self, *args, **kwargs):
def rpartition(self, *args, **kwargs):
def split(self, *args, **kwargs):
def rsplit(self, *args, **kwargs):
def splitlines(self, *args, **kwargs):
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines()
['ab c', '', 'de fg', 'kl']
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
['ab c\n', '\n', 'de fg\r', 'kl\r\n']

str.join(iterable) 用指定的字符串,连接元素为字符串的可迭代对象。若為字符串,則每個字符間均插入,若為 "abc" , "123" 等()[]{}集合,則將子集作連結 abc_123

def join(self, ab=None, pq=None, rs=None):

其他

str.encode(encoding=”utf-8″, errors=”strict”)

def encode(self, *args, **kwargs):

  

Python开发 基礎知識 3.類別&方法 (bool & str) (未完待續)的更多相关文章

  1. Python开发 基礎知識 (未完代補)

    一.Python基本知識 1.Python屬高階語言,所編築的是字節碼 2.一般狀態statement 終止於換行,如需使用多數行編寫,可在行末加上 \,以表延續 但在 parentheses ( ) ...

  2. Python开发 基礎知識 2.變量 ( *arg, **kwargs )

    變量 *args 和 **kwargs ( *和**為本體,名稱為通俗的名稱約定 ) *args 用於函式定義. 可將不定數量的參數傳遞給一個函數,傳入函式的引數,會先以Tuple物件收集,再設定給參 ...

  3. JavaScript基礎知識

    JavaScript基礎知識 1.標籤組使用 <script charset='utf-8' //設置字元集 defet //使腳本延遲到文檔解析完成,Browser已忽略 language=' ...

  4. BootStrap基礎知識

    BootStrap基礎知識 1. .lead //突出 .text-left //文字居左 .text-right //文字居右 .text-center //文字居中 .text-justify / ...

  5. CSS1-3基礎知識

    CSS1-3基礎知識 1.css排版 css在html內排版: <style type='text/css'> 標記名{} .類型名{} #ID名{} 標記名,.類型名,#ID名{} &l ...

  6. jQuery基礎知識

    jQuery基礎知識 $(function(){}) //jQuery先執行一遍再執行其他函數 $(document).ready(fn) //文檔加載完後觸發 1. 刪除$:jQuery.noCon ...

  7. HTML 4.01+5基礎知識

    HTML 4.01+5 1.Html結構:html>head+body 2.Html快捷鍵:!加Tab(在sublime中) 3.雙標籤: ①常用標籤 h1.h2.h3.h4.h5.h6 p.c ...

  8. Linux基礎知識 —— open&close

    下面說一下在用戶空間調用open/close/dup跟驅動中的open和release的對應. 下面是測試驅動: #include <linux/module.h> #include &l ...

  9. Python 基礎 - bytes數據類型

    三元運算 什麼是三元運算?請看下圖說明 透過上圖說明後,可以得出一個三元運算公式: result = 值1 if 條件 else 值2, 如果鯈件為真: result = 值1 如果鯈件為假: res ...

随机推荐

  1. 全局解释器锁GIL & 线程锁

    1.GIL锁(Global Interpreter Lock) Python代码的执行由Python虚拟机(也叫解释器主循环)来控制.Python在设计之初就考虑到要在主循环中,同时只有一个线程在执行 ...

  2. gym 101081 gym F. Auction of Services 最小生成树+倍增LCA

    F. Auction of Services time limit per test 2.0 s memory limit per test 256 MB input standard input o ...

  3. java笔记 -- java简单结构代码解析及注释

    结构代码解析 public class FirstSample { public static void main(String[] args) { System.out.println(2.0-1. ...

  4. MongoDB基本增删改查

    一. 在Node中如何操作 MongoDB数据  1.使用官方的 mongodb 来操作:https://github.com/mongodb/node-mongodb-native  2.使用第三方 ...

  5. python中mysql数据库的操作-sqlalchemy

    MySQLdb支持python2.*,不支持3.* ,python3里面使用PyMySQL模块代替 python3里面如果有报错  django.core.exceptions.ImproperlyC ...

  6. MVC实战之排球计分(五)—— Controller的设计与实现

    控制器 控制器接受用户的输入并调用模型和视图去完成用户的需求.所以当单击Web页面中的超链接和发送HTML表单时, 控制器本身不输出任何东西和做任何处理.它只是接收请求并决定调用哪个模型构件去处理请求 ...

  7. 使用blas做矩阵乘法

      #define min(x,y) (((x) < (y)) ? (x) : (y)) #include <stdio.h> #include <stdlib.h> # ...

  8. 朋友给的IE滚动条

    scrollbar-arrow-color: #f4ae21;  /*图6,三角箭头的颜色*/scrollbar-face-color: #333;  /*图5,立体滚动条的颜色*/scrollbar ...

  9. API的控制器

    // GET: api/showApi /// <summary> /// 显示 查询 /// </summary> /// <param name="name ...

  10. vue 和react的区别

    1.数据是不是可变的 react整体是函数式的思想,把组件设计成纯组件,状态和逻辑通过参数传入,所以在react中,是单向数据流,推崇结合immutable来实现数据不可变. react在setSta ...