[ Python ] 基本數據類型及屬性(上篇)

1. 基本數據類型

        (1) 數字 - int
        (2) 字符串 - str
        (3) 布爾值 - boolpython

2. int 類型中重要的方法  

   (1) int
        將字符串轉換爲數字類型:
    
    # 將字節爲數字的字符串轉換爲 int 類型git

# 將字節爲數字的字符串轉換爲 int 類型
a = '123'
b = int(a)
print(type(a), a)
print(type(b), b)
	
# 用 十六進制的方式將 num 轉換爲十進制
num = '0011'
v = int(num, base=16)
print(v)

 

3. 字符串主要的方法  

  

 

 實例詳細介紹:api

 

  (1) capitalize()
        首字母大寫ide

test = 'hkey'
v = test.capitalize()
print(v)

# 執行結果:
Hkey

 

     (2) lower() casefold()
    將字符串大寫字母變小寫,casefold() 可將其餘國家的一些字母變小寫spa

test = 'HkEy'
v1 = test.casefold()
v2 = test.lower()
print(v1, v2)

# 執行結果:
hkey hkey

 

     (3) center()
    設置寬度,並將內容居中, 20 代指總長度; *  代指空白填充code

name = 'hkey'

v3 = name.center(20,'#')
print(v3)

# 執行結果:
########hkey########

 

     (4) count()
    在字符串中尋找子序列出現的個數orm

name = 'hkeyxiaoxiao'

v = name.count('x')
print(v)

# 執行結果:
2

# 可設置起始位置和結束位置

name = 'hkeyxiaoxiao'
v1 = name.count('x', 0, 8)
print(v1)

# 直接結果:
1

 

     (5) startswith() endswith()
  startswith():已什麼序列開頭,結果爲布爾值
  endswith(): 以什麼序列結尾,結果爲布爾值blog

 

name = 'hkey'

v = name.startswith('h')
print(v)

# 執行結果:
True


v1 = name.endswith('y')
print(v1)

# 執行結果:
True

 

     (6) find() rfind()
    從開始日後找,找到第一個,獲取其索引, 結果爲: -1  表示沒找到索引

name = 'hkeykey'

# 從開始找第一個匹配的序列,並打印序列起始的索引位置
v1 = name.find('key')
print(v1)

# 執行結果:
1

# (sub, start=None, end=None) start:起始位置 end: 結束位置
v2 = name.find('key', 0, 3)
print(v2)

# 執行結果:
-1

name = 'khkeykey'
# 從右到左查找字符索引位置
print(name.rfind('y'))

# 執行結果:
# 7

 

    (7) format() format_map()
    format() 格式化,將一個字符串中指定的佔位符替換爲值,佔位符用 {} 表示 format_map() 格式化,經過字典的形式將值傳給對應 key 的佔位符ip

# 格式化,將一個字符串中指定的佔位符替換爲值
test = 'i am {name}, age {a}'
print(test)
# 執行結果:
i am {name}, age {a}

v = test.format(name='hkey', a=20)
print(v)
# 執行結果:
i am hkey, age 

# 可以使用索引直接指定佔位符
test = 'i am {0}, age {1}'
print(test)

# 執行結果:
i am {0}, age {1}

v = test.format('hkey', 20)
print(v)

# 執行結果:
i am hkey, age 20

# format_map 經過字典的形式將值傳給對應 key 的佔位符
test = 'i am {name}, age {a}'

v1 = test.format_map({'name': 'hkey', 'a': 20})

print(v1)
	
# 執行結果:
# i am hkey, age 20

 

    (8) index()
    從開始日後找,找到第一個,獲取其索引, 若是沒有就報錯。

name = 'hkey'
v = name.index('y')
print(v)
# 執行結果:
# 3

v1 = name.index('z')
print(v1)

# 執行結果:
# Traceback (most recent call last):
#   File "E:/learn_python/day11/s2.py", line 119, in <module>
#     v1 = name.index('z')
# ValueError: substring not found

 

    (9) isalnum
    字符串中是否只包含 字母和數字

test = 'abcd+_'
v = test.isalnum()
print(v)

# 執行結果:
# False

test = 'abcd'
v = test.isalnum()
print(v)

# 執行結果:
# True

 

    (10) expandtabs
    若是字符串中含有製表符 ' \t ' ,則做爲製表符來分割字符串。

s = 'username\temail\tpassword\nhkey\thkey@qq.com\thkeyy'
v = s.expandtabs(20)
print(v)

# 執行結果:
# username            email               password
# hkey                hkey@qq.com         hkeyy

 

    (11) isalpha()
    判斷字符串是否包含數字,包含數字爲 False,不包含數字爲: True

s = 'superman'
v = s.isalpha()
print(v)

# 執行結果:
# True

 

    (12) isdecimal()  isdigit() isnumeric()
    判斷字符串是否爲數字
    isdigit() 能識別特殊符號的數字寫法
    isnumeric() 可以判斷中文的數字寫法 ‘二’

test = '②'

v1 = test.isdecimal()
v2 = test.isdigit()
print(v1, v2)

# 執行結果:
# False True

test1 = '二'
v1 = test1.isdecimal()
v2 = test1.isdigit()
# 可以判斷中文數字的寫法
v3 = test1.isnumeric()
print(v1, v2, v3)

# 執行結果:
# False False True

 

    (13) islower()
    判斷字符串小寫。

test='hkey'
v=test.islower()
print(v)

#執行結果:
#True

 

    (14) isprintable()
    判斷字符串中是否含有不可顯示的字符,如 \t \n 等

test = 'abcdefg\t'
v = test.isprintable()
print(v)

# 執行結果:
# False

 

    (15) isspace()
    判斷變量是否所有爲空格

test = ' '
v = test.isspace()
print(v)

# 執行結果:
# True

 

    (16) istitle()  title()
    istitle() 判斷是否爲首字母都是大寫的字符串
    title() 將字符串轉換爲首字母大寫的標題

test = 'my heart will go on'

v = test.istitle()
v1 = test.title()
print(v)
print(v1)

# 執行結果:
# False
# My Heart Will Go On

 

    (17) join()
    將字符串中的每一個元素按照指定的分隔符進行拼接

test = '看不見你的笑我怎麼睡得着'
v = '#'.join(test)
print(v)

# 執行結果:
# 看#不#見#你#的#笑#我#怎#麼#睡#得#着

 

    (18) ljust() rjust()
    設置寬度:
        ljust() 字符串放置左邊
        rjust() 字符串放置右邊

name = 'hkey'

v1 = name.ljust(20,'*')
v2 = name.rjust(20, '*')
print(v1)
print(v2)

# 執行結果:
# hkey****************
# ****************hkey

 

    (19) zfill()
    不能指定字符,只是 0 填充到左邊

name = 'hkey'
v1 = name.zfill(20)
print(v1)

# 執行結果:
# 0000000000000000hkey

 

    (20) isupper() upper()
    upper() 將小寫字符串轉換爲大寫
    isupper() 判斷字符串是否爲大寫

test = 'my heart will go on'

v1 = test.isupper()
v2 = test.upper()
print(v1)
print(v2)

# 執行結果:
# False
# MY HEART WILL GO ON

 

    (21) lstrip() rstrip() strip()
    lstrip() 去除字符串首部特殊符號及空格
    rstrip() 去除字符串尾部特殊符號及空格
    strip() 去除字符串首尾及空格

name = '\nhkey\n'

v1 = name.lstrip()
v2 = name.rstrip()
v3 = name.strip()
print(v1)
print(v2)
print(v3)

# 執行結果:
# v1:
# hkey
# 
# v2:
# 
# hkey
# v3:
# hkey

 

    (22) maketrans()
    translate() maketrans()  將兩個一一對應的字符串進行替換
    translate() 替換maketrans中兩個字符串

test1 = 'abcdefg'
test2 = '1234567'

v = 'adfasdfzcvdrfhkljwerto'
m = str.maketrans(test1, test2)
new_m = v.translate(m)
print(new_m)

# 執行結果:
# 1461s46z3v4r6hkljw5rto

 

    (23) partition()  rpartition()  split()  rsplit()
    partition() 將字符串分割爲三分,並將分隔符做爲獨立的元素進行分割
    rpartition() 從右邊開始,將字符串分割爲三分,並將分隔符做爲獨立的元素進行分割
    split() 用指定的字符分割字符串,分割後的列表中不包含分割的字符,可執行分割次數
    rsplit() 從右邊開始,用指定的字符分割字符串,分割後的列表中不包含分割的字符,可執行分割次數

test = 'asdfadfsdfxzscv'

# 將字符串分割爲三分,並將分隔符做爲獨立的元素進行分割
v = test.partition('s')
print(v)

# 從右邊開始,將字符串分割爲三分,並將分隔符做爲獨立的元素進行分割
v1 = test.rpartition('s')
print(v1)

# 用指定的字符分割字符串,分割後的列表中不包含分割的字符,可執行分割次數
v2 = test.split('s', 1)
print(v2)
# 從右邊開始,用指定的字符分割字符串,分割後的列表中不包含分割的字符,可執行分割次數
v3 = test.rsplit('s', 1)
print(v3)

# 執行結果:
# 
# v:
# ('a', 's', 'dfadfsdfxzscv')
# v1:
# ('asdfadfsdfxz', 's', 'cv')
# v2:
# ['asdfadfsdfxz', 'cv']
# v3:
# ['a', 'dfadfsdfxzscv']
# v4:
# ['asdfadfsdfxz', 'cv']

 

    (24) splitlines()
    分割,只能根據:True、False 是否保留換行

test = 'adfaf\nadfadf\nadfaf\n'
v = test.splitlines(True)
v1 = test.splitlines(False)
print(v)
print(v1)

# 執行結果:
# v:
# ['adfaf\n', 'adfadf\n', 'adfaf\n']
# v1:
# ['adfaf', 'adfadf', 'adfaf']

 

(25) startswith()  endswith()
    startswith: 以什麼開頭
    endswith: 以什麼結尾

test = 'hkey'

# 以什麼開頭
v1 = test.startswith('h')
# 以什麼結尾
v2 = test.endswith('e')
print(v1)
print(v2)

# 執行結果:
# True
# False

 

    (26) swapcase()
    大小寫轉換

name = 'HkEy'
v = name.swapcase()

print(v)

# 執行結果:
# hKeY

 

    (27) isidentifier()
    檢測字符串是不是字母開頭

test = '1a1dsf123'
print(test.isidentifier())

# 執行結果;
# False

 

  (28) replace()
  替換字符串

name = 'hkeykey'
# 將字符串中的 'k' 替換爲 'f' 最多替換1次
print(name.replace('k', 'f', 1))

# 執行結果:
# hfeykey

 

總結:

字符串中幾個經常使用的屬性:

  join() 、 split() 、 find() 、 strip() 、 upper() 、 lower() 、lower()

 

4. 經常使用的字符串操做

(1) 經過索引獲取字符

name = 'hkey'
print(name[2])

# 執行結果:
# e

 

(2) 切片

  經過索引的起始值、結束值、步長 來切分字符串

name = 'hkey'
v1 = name[0:2]
v2 = name[0:4:2]
print(v1)
print(v2)

# 執行結果:
# v1:
# hk
# v2:
# he

 (3) 獲取字符串的長度

name = 'hkey'
print(len(name))

# 執行結果:
# 4

 

5. 操做字符串解析

字符串在內存中一旦建立就沒法被修改,若是對字符串進行修改或者拼接,必然會生成一個新的字符串

相關文章
相關標籤/搜索