1. 程式人生 > >數據類型,字符編碼,文件處理

數據類型,字符編碼,文件處理

ati nbsp mat print {} asdfasdf 描述 login 玩法

1.數據類型:
  數字(整型,長整型,浮點型,復數)
  字符串:在介紹字符編碼時介紹字節bytes類型
  列表
  元祖
  字典
  集合

2.整型 int
  作用:年級/等級/身份證號等整型數字相關
  定義: age=10  本質上age=int(10)

十進制轉成。。。進制
print(bin(13))    #將整型轉變成二進制
print(oct(13))    #將整型轉變成八進制
print(hex(13))    #將整型轉變成十六進制

常用操作+內置方法

# 存一個值

# 不可變
# x=10
# print(id(x))
# x=11
# print(id(x))

 3. 浮點型 float

  作用:薪資/身高/體重等浮點數相關

 salary=3000.3 #本質salary=float(3000.3)

類型轉換

print(float(10))
print(float(1.1))
print(float(1.1))

4.字符串類型 str

  作用:記錄描述性值的狀態,比如名字/性別等

msg=hello world #msg=str(‘hello world‘)

類型轉換:可以把任意類型轉成字符串類型

res1=str(10)
res2=str(10.3)
res3=str([1,2,3])
res4=str({
x:1}) #res4="{‘x‘:1}"

*****常用操作+內置的方法

1.按索引取值(正向取+反向取):只能取

msg=hello world

print(type(msg[0]))
print(msg[-1])

msg[0]=H

2.切片(顧頭不顧尾,步長)

msg=hello world
print(msg[0]+msg[1]+msg[2])
print(msg[0:5])
print(msg[0:5:2]) 
print(msg[0:]) 
print(msg[:]) 

print(msg[-1:-5:-1]) #-1 -2 -3 -4
print
(msg[::-1]) #-1 -2 -3 -4

3.長度 len :統計的是字符的個數

4.成員運算 in 和 not in : 判斷一個子字符是否存在於一個大字符串中

# msg=‘hello world‘
# print(‘ho‘ in msg)
# print(‘ho‘ not in msg)

5.移除空白 strip : 移除字符串左右兩邊的某些字符

msg=      hello      

print(msg.strip( ))
print(msg.strip())
print(msg)

name=input(name>>>: ).strip() #name=‘egon‘
pwd=input(password>>>: ).strip()

if name == egon and pwd == 123:
    print(login successfull)
else:
    print(username or password error)

msg=***h**ello**********
print(msg.strip(*))

msg=*-=+h/ello*(_+__
print(msg.strip(*-=+/(_))

6.切分 split :把有規律的字符串切成列表從而方便取值

info=egon:18:180:150
res=info.split(:,1)
print(res)
print(res[1])


info=egon:18:180:150
res=info.split(:)
print(res)


s1=res[0]+:+res[1]+:+res[2]+:+res[3]
s1=‘‘
for item in res:
    s1+=item
print(s1)


s1=:.join(res)
print(s1)

:.join([1,2,3,4,5])

7.循環

for i in hello:
    print(i)

****需要掌握的操作

1.strip , lstrip , rstrip

msg=*****hello****
print(msg.strip(*))
print(msg.lstrip(*))
print(msg.rstrip(*))

2.lower , upper

msg=AaBbCc123123123
print(msg.lower())
print(msg.upper())

#執行之後
#aabbcc123123123
#AABBCC123123123

3. startswith , endswith

msg=alex is dsb
print(msg.startswith(alex))
print(msg.endswith(sb))

#執行之後
#True
#True

4.format 的三種玩法

msg=my name is %s my age is %s %(egon,18)
print(msg)

#執行之後
#my name is egon my age is 18
msg=my name is {name} my age is {age}.format(age=18,name=egon)
print(msg)

#執行之後
#my name is egon my age is 18

msg=my name is {} my age is {}.format(18,egon)
msg=my name is {0}{0} my age is {1}{1}{1}.format(18,egon)
print(msg)

#執行之後
#my name is 1818 my age is egonegonegon

5.split , rsplit

cmd=get|a.txt|33333
print(cmd.split(|,1))
print(cmd.rsplit(|,1))

#執行之後
#[‘get‘, ‘a.txt|33333‘]
#[‘get|a.txt‘, ‘33333‘]

6. replace

msg=kevin is sb kevin kevin
print(msg.replace(kevin,sb,2))

#sb is sb sb kevin

7. isdigit (當字符串內為純數字時結果為True

res=11111
print(res.isdigit())
int(res)

#True

age_of_bk=18
inp_age=input(your age: ).strip()
if inp_age.isdigit():
    inp_age=int(inp_age) #int(‘asdfasdfadfasdf‘)
    if inp_age > 18:
        print(too big)
    elif inp_age < 18:
        print(to small)
    else:
        print(you got it)
else:
    print(必須輸入純數字)

** (了解)

1.find, rfind, index, rindex, count

find 和 index用法差不多,find比index功能強大,優先使用find

print(xxxkevin is sb kevin.find(kevin))
print(xxxkevin is sb kevin.index(kevin))
print(xxxkevin is sb kevin.rfind(kevin))
print(xxxkevin is sb kevin.rindex(kevin))

#執行之後
3
3
15
15

print(kevin is kevin is kevin is sb.count(kevin))

#執行之後
# 3

2. center, ljust ,rjust , zfill

print(egon.center(50,*))
print(egon.ljust(50,*))
print(egon.rjust(50,*))
print(egon.zfill(50))

#執行之後
***********************egon***********************
egon**********************************************
**********************************************egon
0000000000000000000000000000000000000000000000egon

3. captalize , swapcase , title

print(my name is kevin.capitalize())
print(AaBbCc.swapcase())
print(my name is kevin.title())

#執行之後
My name is kevin
aAbBcC
My Name Is Kevin

4.is其他

name=egon123
print(name.isalnum()) #字符串由字母或數字組成
print(name.isalpha()) #字符串只由字母組成

print(name.islower())
print(name.isupper())
name=    
print(name.isspace())
msg=I Am Egon
print(msg.istitle())


#執行之後
True
False
True
False
True
True

數據類型,字符編碼,文件處理