1. 程式人生 > >數字類型和字符串類型

數字類型和字符串類型

log 對齊 不可 git alt 常用 orm 顯示 end

1.bin()函數將十進制轉換成而進制

2.oct()函數將十進制轉換成八進制

3.hex()函數將十進制轉換成十六進制

    十六進制表示:0-9 a b c d e f

4.數字類型的特性:

    只能存放一個值

    一經定義,不可更改

    直接訪問

分類:整型,布爾,浮點,復數

5.字符串類型

  引號包含的都是字符串類型

    S1=‘hello world‘ s="hello world"

    s2="""hello world"""

    s3=‘‘‘hello world‘‘‘

  單引雙引沒有區別

6.字符串的常用操作

  strip()移除空白,也可以去除其他的字符

  slipt()分割,默認以空格分割。也可以以其他的字符分割

  len()長度 切片:如print(x[1:3])也是顧頭不顧尾

        print(x[0:5:2])#0 2 4

  capitalize()首字母大寫

  center()居中顯示例如:x=‘hello‘ print(x.center(30,‘#‘))

  count():計數,顧頭不顧尾,統計某個字符的個數,空格也算一個字符

  endswith()以什麽結尾

   satrtswith()以什麽開頭

  find()查找字符的索引位置,如果是負數,代表查找失敗

  index()索引

  find()和index()的區別,如下圖:

      技術分享圖片

  format()字符串格式化

     1.msg=‘name:{},age:{},sex:{}‘

     print(msg.format(‘haiyan‘,18,女))

     2.msg=‘name:{0},age:{1},sex:{0}‘

     print(msg.format(‘aaaaaa‘,‘bbbbbb‘))

    3.msg=‘name:{x},age:{y,sex:{z}‘

     print(msg.format(x=‘haiyan‘,y=‘18‘,z=‘女‘))

  isdigit()判斷是否是數字

  islower()判斷是否是全部小寫

  isupper()判斷是否是全部大寫

  lower()全部轉換為小寫

  upper()全部轉換為大寫

  isspace()判斷是否是全都是空格

  istitle()判斷是否是標題(首字母大寫)

  swapcase()大小寫字母翻轉

  join()連接

  repalce()替換

     msg=‘hello alex‘

     print(msg.replace(‘e‘),‘A‘,1)

     print(msg.replace(‘e‘),‘A‘,2)

   ljust()左對齊

     X=‘ABC‘ print(x.ljust(10,‘*‘))

數字類型和字符串類型