python基礎-基本數據類型總結_整型(int)_字符型(str)_day3


 一、基本數據類型

1、整型(int)

ps1:
數字 int ,所有的功能,都放在int里
a1 = 123
a1 = 456

 

ps2:

int 將字符串轉換為數字

1 # -int
2 # 將字符串轉換為數字
3 a = "123"
4 b = int(a)
5 b = b + 1000
6 print(b)

執行結果:

1 1123

 

ps3:

這種類型,不能轉換

1 a = "123a"   #不能轉換,會報錯
2 b = int(a)
3 print(b)

執行結果:

1 Traceback (most recent call last):
2   File "D:/python/day3/s4.py", line 14, in <module>
3     b = int(a)
4 ValueError: invalid literal for int() with base 10: '123a

 

ps4:

type 查看他是什么數據類型

1 a = "123"
2 print(type(a))
3 b = int(a)
4 print(type(b))

執行結果:

1 <class 'str'>
2 <class 'int'>

 

ps5:

將字符串轉換為數字

1 a = "123"
2 print(type(a),a)
3 
4 b = int(a)
5 print(type(b),b)

執行結果:

1 <class 'str'> 123
2 <class 'int'> 123


ps6:
字符串以二進制的方式進行轉換

1 num = "0011"  #這個字符串以二進制的方式進行轉換
2 v = int(num, base=2)
3 print(v)

執行結果:

3

 

ps7:

字符串以十六進制的方式進行轉換

1 num = "a"  #這個字符串以十六進制的方式進行轉換
2 v = int(num, base=16)
3 print(v)

執行結果:

1 10

 

ps8:

字符串以十六進制的方式進行轉換

1 num = "0011"  #這個字符串以十六進制的方式進行轉換
2 v = int(num, base=16)
3 print(v)

執行結果:

1 17

 

ps9:

bit_lenght
當前數字的二進制,至少用n位表示

 1 # 當前數字的二進制,至少用n位表示
 2 # 1 1
 3 # 2 10
 4 # 3 11
 5 # 4 100
 6 # 5 101
 7 #當前數字的二進制,至少用n位來表示
 8 #age = 1
 9 #age = 2
10 
11 age = 5
12 r = age.bit_length()
13 print(r)

執行結果:

1 3

 


 2、字符串(str)

ps1:

capitalize 首字母大寫

1 test = "aLex"
2 v = test.capitalize()
3 print(v)

執行結果:

Alex

 

ps2:

lower ,casefold

所有變小寫,casefold更牛逼,很多未知的對相應變小寫

1 test = "aLex"
2 v1 = test.casefold()  #casefold更牛逼,很多未知的對相應變小寫 3 print(v1)
4 v2 = test.lower()     #只能處理普通英文的字符,歐洲的特殊字符處理不了
5 print(v2)

執行結果:

1 alex
2 alex

 

ps3:

3 設置寬度,並將內容居中
20 代指總長度
* 空白未知填充,一個字符,可有可無

語法:def center(self,width,fillchar=None)  #后面接參數:1、直接忽略  2、必須帶  3、有等號的是可選項(可帶可不帶,如果不設置就用默認:None參數)

1、cneter居中 左右兩邊空格填充(總共長度20位,不夠的左右兩邊空格填充)

test = "aLex"
v = test.center(20)
print(v)

執行結果:

1         aLex          #總長度20位,不夠左右兩邊空格填充

 

2、cneter居中 左右兩邊*號填充(總共長度20位,不夠的左右兩邊*號填充)

1 test = "aLex"
2 v = test.center(20,"*")
3 print(v)

執行結果:

1 ********aLex********

 

3、center居中 左右兩邊中字填充(總共20位,不夠的左右兩邊中字填充)

1 test = "aLex"
2 v = test.center(20,"")
3 print(v)

執行結果:

1 中中中中中中中中aLex中中中中中中中中

 

4、ljust 右邊以*填充(總共20位)

1 test = "alex"
2 v = test.ljust(20,"*")
3 print(v)

執行結果:

1 alex****************

 

5、rjust 左邊以*填充 (總共20位)

1 test = "alex"
2 v = test.rjust(20,"*")
3 print(v)

執行結果:

1 ****************alex

 

6、zfill 左邊以0填充(總共20位)

1 test = "alex"
2 v = test.zfill(20)
3 print(v)

執行結果:

1 0000000000000000alex

 

ps4:

1、count 去字符串中尋找,尋找子序列的出現次數

1 test = "aLexalexr"
2 v = test.count('ex')   #計算ex 出現的次數
3 print(v)

執行結果:

1 2

 

2、count 5,6(表示起始位置,結束位置)

語法:def count(self,sub,start=None,end=None)

1 test = "aLexalexr"
2 v = test.count('ex',5,6)  #參數中的5,6表示,從那開始到那結束)
3 print(v)

執行結果:

 

1 0

 

 

ps5:

encode 和 decode (這兩個沒有講,先欠着,后面講到的時候再充補)
1、encode
2、decode

 

ps6:

1、startswith 以什么什么開始

1 test = "alex"
2 v = test.startswith('ex')
3 print(v)

執行結果:

1 False

 

2、endswith  以什么什么結尾

1 test = "alex"
2 v = test.endswith('ex')
3 print(v)

執行結果:

1 True

 

ps7:

1、用法:expandtabs 斷句 6

1 # !/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 # Author: nulige
4 
5 test = "12345678\t9"   #123456:6位    四個空格+78:\t補齊4位+78  9:如果后面沒有6位,就不管了
6 v = test.expandtabs(6) #6就是指每6位一段,\t:就是不夠六位的用空格補齊
7 print(v,len(v))        #len :判斷字符串的長度

執行結果:

1 12345678    9 13

 

2、expandtabs,斷句20,

1 # !/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 # Author: nulige
4 
5 # expandtabs,斷句20,
6 #輸出三個\n 就換行 內容就是輸出三行。
7 test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123"
8 v = test.expandtabs(20)
9 print(v)

執行結果:

1 username            email               password
2 laiying             ying@q.com          123
3 laiying             ying@q.com          123
4 laiying             ying@q.com          123

 

ps8:

1、find 從開始往后找,找到第一個之后,獲取其位置

> 或 >=

未找到 -1

test = "alexalex"
v = test.find('ex')  #從前向后找,獲取其位置
print(v)

執果結果:

1 2

 

2、index找不到,報錯 忽略

1 test = "alexalex"
2 v = test.index('8')
3 print(v)

執行結果:

1 Traceback (most recent call last):
2   File "D:/python/day3/s3.py", line 137, in <module>
3     v = test.index('8')
4 ValueError: substring not found

 

ps9:

1、format 格式化,將一個字符串中的占位符替換為指定的值

1 test = 'i am {name}, age {a}'
2 v = test.format(name='alex',a=19)
3 print(v)

執行結果:

1 i am alex, age 19

 

2、用數字,可以直接傳值

1 test = 'i am {0}, age {1}'
2 v = test.format('alex',19)
3 print(v)

執行結果:

1 i am alex, age 19

 

ps10:

format 格式化,傳入的值 {"name": 'alex', "a": 19}

1 test = 'i am {name}, age {a}'
2 v1 = test.format(name='df',a=10)
3 v2 = test.format_map({"name": 'alex',"a": 19})  #這兩種格式結果是一樣的,只是寫法不同
4 print(v1)
5 print(v2)

執行結果:

1 i am df, age 10
2 i am alex, age 19

 

ps11:

isalnum  判斷字符串中只能包含字母和數字,如果是就是True

1 test = "jjj123"
2 v = test.isalnum()   
3 print(v)

執行結果:

1 True

 

ps12:

isalpha 判斷字符串中是否是字母和漢子(如果不是就是Flase)

1 test = "asdf"
2 v = test.isalpha()
3 print(v)
1 True

 

ps13:

1、iddecimal ,isdigit,isnumeric 判斷當前輸入的值,是否是數字

1 test = ""  #這種特殊的數字
2 v1 = test.isdecimal()   #判斷是否是數字
3 v2 = test.isdigit()     #判斷是否是數字,還可以判斷特殊的數字! 4 print(v1,v2)

執行結果:

1 False True

 

2、isnumeric 判斷當前輸入是否是數字

1 test = "" # 1,②
2 v3 = test.isnumeric()
3 print(v3)

執行結果:

1 True

 

ps14:

1、isidentifier 判斷字母,數字,下划線:標識符:def class

1 a = "_123"
2 v = a.isidentifier()
3 print(v)

執行結果:

1 True

 

2、isidentifier 判斷字母,數字,下划線:標識符:def class

1 a = "def"  #標識符也符合
2 v = a.isidentifier()
3 print(v)

執行結果:

1 True

 

3、isidentifier 判斷字母,數字,下划線:標識符:def class

1 a = "123"  #不符合,沒有下划線
2 v = a.isidentifier()
3 print(v)

執行結果:

1 False

 

ps15:

isprintable 是否存在不可顯示的字符

\t 制表符
\n 換行
1 test = "oiuas\tdfkj"
2 v = test.isprintable()
3 print(v)

執行結果:

1 False

 

ps16:

isspace 判斷是否全部是空格

1 test = " "    
2 v = test.isspace()  
3 print(v)

執行結果:

1 True

 

ps17:

istitle, title,判斷是否是標題

1 test = "Return True if all cased characters in S are uppercase and there is"
2 v1 = test.istitle()  #判斷標題,首字母是否大寫
3 print(v1)
4 v2 = test.title()    #把普通的字符串轉換為首字母大寫
5 print(v2)
6 v3 = v2.istitle()    #再判斷v2,就是大寫啦。所以是True
7 print(v3) 

執行結果:

1 False
2 Return True If All Cased Characters In S Are Uppercase And There Is
3 True

 

 ps18:   (標記為五星表示經常用到的)

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

1 test = "你是風兒我是沙"
2 v = "_".join(test)
3 print(v)

執行結果:

1 你_是_風_兒_我_是_沙

 

ps19:

判斷是否全部是大小寫 和 轉換為大小寫

1、islower 判斷是否是小寫 和 lower轉換為小寫

1 test = "Alex"
2 v1 = test.islower()   #判斷是否是小寫; 用於驗證碼:不管用戶輸入的是大寫或小寫,統一轉換為小寫
3 v2 = test.lower()     #轉換為小寫
4 print(v1, v2)

執行結果:

1 False ALEX

 

2、isupper判斷是否是大寫和upper 轉換為大寫

1 test = "Alex"
2 v1 = test.isupper()   #判斷是否是大寫
3 v2 = test.upper()     #轉換為大寫
4 print(v1,v2)

執行結果:

1 False ALEX

 

ps20:

1、lstrip,rstrip,strip去除左右空白

1 test = " alex "
2 v1 = test.lstrip()       #處理左邊的空格
3 v2 = test.rstrip()       #處理右邊的空格
4 v3 = test.strip()        #兩邊都處理空格
5 print(v1,v2,v3)

執行結果:

1 alex   alex alex

 

2、去除\t  \n

1 test = "\nlex "
2 v1 = test.lstrip()       #處理左邊的\n or \t
3 print(test)
4 print(v1)

執行結果:

1     #空格
2 lex 
3 lex 

 

3、去除左邊的x (移除最多字符)

1 test = "xalex "
2 v1 = test.lstrip('x')       #處理左邊的x
3 print(v1)

執行結果:

1 alex 

 

4、rstrip 從右邊開始往左邊找,先進行最多匹配

1 test = "xalex"
2 v1 = test.rstrip('91lexex')   #從右邊開始往左邊找,先進行最多匹配
3 print(v1)

執行結果:

1 xa

 

ps21:

translate 對應關系替換

1 v = "asidufkasd;fiuadkf;adfkjalsdjf"
2 m = str.maketrans("aeiou", "12345")   #定義a=1,e=2,其他依此類推
3 new_v = v.translate(m)
4 print(new_v)

執行結果:

1 1s3d5fk1sd;f351dkf;1dfkj1lsdjf

 

ps22:

1、partition,rpartition 分割為三部分

1 test = "testasdsddfg"
2 v = test.partition('s')  #找到第1個s進行分割,只能分成三部分
3 print(v)
4 
5 test = "testasdsddfg"
6 v = test.rpartition('s')
7 print(v)

執行結果:

1 ('te', 's', 'tasdsddfg')  #第一個的結果
2 ('testasd', 's', 'ddfg')  #第二個的結果

 

2、split, rsplit 分割為指定個數,分割后s拿不到

  • 正則表達表 (以后會學)
  • 是否想要分割的元素
  • 用途:計算器
1 test = "testasdsddfg"
2 v = test.split('s',2)
3 print(v)
4 
5 test = "testasdsddfg"
6 v = test.rsplit('s',2)
7 print(v)

執行結果:

1 ['te', 'ta', 'dsddfg']
2 ['testa', 'd', 'ddfg']

 

3、partition 和 split 的區別

答:區別就是分割后一個可以拿到*,一個不能拿到*

1 test = "1*2"
2 v = test.partition('*')
3 print(v)
4 
5 test = "1*2"
6 v = test.split('*',2)
7 print(v)

執行結果:

1 ('1', '*', '2')  #拿到了*
2 ['1', '2']       #沒有拿到*

 

ps23:

splitlines 分割,只能根據,true,false:是否保留換行

 1 test = "asdfadfasdf\nasdfasdf\nadfasdf"
 2 v = test.splitlines()
 3 print(v)
 4 
 5 test = "asdfadfasdf\nasdfasdf\nadfasdf"
 6 v = test.splitlines(True)
 7 print(v)
 8 
 9 test = "asdfadfasdf\nasdfasdf\nadfasdf"
10 v = test.splitlines(False)  #參數只能加True or False
11 print(v)

執行結果:

1 ['asdfadfasdf', 'asdfasdf', 'adfasdf']              #不加參數
2 ['asdfadfasdf\n', 'asdfasdf\n', 'adfasdf']          #True
3 ['asdfadfasdf', 'asdfasdf', 'adfasdf']              #False

 

ps24:

1、startswith 判斷以xxx開頭

1 test = "backend 1.1.1.1"
2 v = test.startswith('b')     #xxx開頭
3 print(v)

執行結果:

1 True

 

2、endswith 判斷以xxx結尾

1 test = "backend 1.1.1.1"
2 test.endswith('1')           #以xxx結尾
3 print(v)

執行結果:

1 True

 

ps25:

swapcase 大小寫轉換

1 test = "aLex"
2 v = test.swapcase()
3 print(v)

執行結果:

1 AlEX

 

ps26:

1、replace 將指定字符串替換為指定字符串

1 test = "alexalexalex"
2 v = test.replace("ex",'bbb')
3 print(v)

執行結果:

1 albbbalbbbalbbb

 

2、replace 將指定字符串替換為指定字符串,指定個數

1 test = "alexalexalex"
2 v = test.replace("ex",'bbb',2)
3 print(v)

執行結果:

1 albbbalbbbalex

 


 重點:

一、七個基本魔法如下:

  1. join
  2. split
  3. find
  4. strip
  5. upper
  6. lower
  7. replace

二、4個灰魔法如下:

test = "鄭建文妹子有種沖我來"

1、for循環

 1 for 變量名 in 字符串:
 2 變量名
 3 break
 4 continue
 5 
 6 index = 0
 7 while index < len(test):
 8 v = test[index]
 9 print(v)
10 
11 index += 1
12 print('=======')
13 
14 for zjw in test:
15 print(zjw)
16 
17 test = "鄭建文妹子有種沖我來"
18 for item in test:
19 print(item)
20 break
21 
22 for item in test:
23 continue
24 print(item)

 

2、索引,下標,獲取字符串中的某一個字符

1 v = test[3]
2 
3 print(v)

 

3、切片

1 v = test[0:-1]    # 0=< <1
2 print(v)

 

4、獲取長度

Python3: len獲取當前字符串中由幾個字符組成v = len(test)print(v)

1 v = len(test)
2 print(v)

 

注意:

1 len("asdf")
2 for循環
3 索引
4 切片

 

 5、獲取連續或不連續的數字,

 1 Python2中直接創建在內容中
 2 python3中只有for循環時,才一個一個創建
 3 r1 = range(10)
 4 r2 = range(1,10)
 5 r3 = range(1,10,2)
 6 
 7 
 8 幫助創建連續的數字,通過設置步長來指定不連續
 9 v = range(0, 100, 5)
10 
11 for item in v:
12 print(item)
13 
14 
15 練習題:根據用戶輸入的值,輸出每一個字符以及當前字符所在的索引位置
16 test = input(">>>")
17 for item in test:
18 print(item)
19 
20 將文字 對應的索引打印出來:
21 test = input(">>>")
22 print(test) # test = qwe test[0] test[1]
23 l = len(test) # l = 3
24 print(l)
25 
26 r = range(0,l) # 0,3
27 for item in r:
28 print(item, test[item]) # 0 q,1 w,2 e
29 
30 test = input(">>>")
31 for item in range(0, len(test)):
32 print(item, test[item])

 

三、1個深灰魔法

記住兩句話:

  1. 字符串一旦創建,不可修改
  2.  一旦修改或者拼接,都會造成重新生成字符串
1 name = "zhengjianwen"
2 age = "18"
3 info = name + age
4 print(info)

執行結果:

1 zhengjianwen18

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM