1. 程式人生 > >python第三天課程:int, bool, str

python第三天課程:int, bool, str

什麼資料型別:

  int :1.2.3 用於計算

# .bit_length()計算二進位制的位數
"""
i = 100
print(i.bit_length())
"""

  bool :True,False 使用者判斷

  str: 儲存少量資料,進行操作

  'fdjsal' , '二哥' , '`13243' , 'fdshklj' , '戰三, 李四, 王二麻子。。。。'

  list:儲存大量的資料。

    [1,2,3 , '泰哥' ,  '12353234' , [1,2,3]]

  元祖:只讀。

    (1,2,3, '第三方',)

  dict:字典{'name' : ’雲姐‘ , 'age' : 16}

    字典{'雲姐' : [] , '二哥'  : [200,200,200,200,。。。。。。]}

  集合: {1,2,34, 'aasdf'}

轉換:

  

#bool True False

#int ----> str
i = 1
s = str(1)

#str ----> int
s = '123'
i = int(s)

#int ----> bool 只要是0 ----> False  非0就是True
i = 3
b = bool(i)
print(b)

#bool ----> int
#True   1
#False  0

'''
ps: while True: pass while 1:效率高 pass ''' #str ----> bool #s=""----> False #非空字串都是True #s = "0"----> True #s if s: print('您輸入的為空,請重新輸入') else: pass

 

字串的索引與切片:

'''
s = 'ABCDLSESRF'
#索引
s1 = s[0]
print(s1)
s2 = s[2]
print(s2)
s3 = s[-1]
print(s3)
s4 = s[-2]
print(s4)
#ABCD  切片:顧頭不顧尾
s = 'ABCDLSESRF'
s5 = s[0:4]
print(s5)
s6 = s[0:-1]
print(s6)
s7 = s[:]
s8 = s[0:]
print(s7, s8)
s9 = s[0:0]
s = 'ABCDLSESRF' # s[首:尾:步長]
s10 = s[0:5:2]
print(s10)
s11 = s[4:0:-1]
print(s11)
s12 = s[3::-1]
print(s12)
s13 = s[3::-2]
print(s13)
s14 = s[-1::-1]
print(s14)
s15 = s[::-1]
print(s15)
'''

字串的操作:

'''
s = 'alexWUsir'
s1 = s.capitalize() #首字母大寫
print(s1)

#全大寫全小寫
s2 = s.upper()
s3 = s.lower()
print(s2,s3)

s_str = 'acEQ1'
you_input = input('請輸入驗證碼,不區分大小寫')
if you_input.lower() == s_str.lower():
    print('輸入成功')
else:
    print('請重新輸入')

#大小寫翻轉
s3 = s.swapcase()
print(s3)

#每個隔開(特殊字元或數字)的單詞首字母大寫
s = 'alex*egon-wusir'
s4 = s.title()
print(s4)
s = 'fade,crazy*w4rri0r_songsong node_3'
s4 = s.title()
print(s4)

#居中,空白填充
s = 'alexWUsir'
s5 = s.center(20,'~')
print(s5)

s = 'alex\tsir'
s6 = s.expandtabs()
print(s6)

s = 'alex二哥'
#公共方法
l =len(s)
print(l)

#以什麼開頭結尾endswith
s = 'alexWUsir'
s7 = s.startswith('alex')
print(s7)
s71 = s.startswith('e',2,5)
print(s71)

if s7:
    pass
elif s.startswith('bl'):
    pass

# find 通過元素找索引,找不到返回-1
#index 通過元素找索引,找不到報錯
s = 'alexWUsir'
s8 = s.find('A')
s9 = s.index('A')
print(s8,s9)

#strip rstrip lstrip
s = 'alexWUsir%'
s9 = s.strip('%')
print(s9)
s = ' *a%lexWUsi* r%'
s91 = s.strip(' %*')
print(s91)

#strip預設刪除前後空格
username = input('請輸入名字: ').strip()
if username == '春哥':
    print('恭喜春哥發財')

s = 'alexaa wusirl'
s10 = s.count('al')
print(s10)

#split       str ----> list
s = ';alex;wusir;taibai'
l = s.split('a')
print(l)

#format的有三種玩法 格式化輸出
s = '我叫{}, 今年{}, 愛好{}, 再説一下我叫{}'.format('太白', 18, 'girl', '太白')
print()
name = input('請輸入名字:')
s = '我叫{0}, 今年{1}, 愛好{2}, 再説一下我叫{0}'.format(name,36, 'girl')
print(s)
name = input('請輸入名字:')
s = '我叫{name}, 今年{age}, 愛好{hobbie}, 再説一下我叫{name}'.format(name = name,age = 36, hobbie ='girl')
print(s)

s = '來看待街坊鄰居複合大師街坊法好的撒見客戶'
s11 = s.replace('街','老王',1)
print(s11)

s = 'fhdsklfds'
for i in s:
    print(i)
s = 'fdsa蒼井空fdsalk'
if '蒼井空' in s:
    print('您的評論有敏感詞...')

name = 'jinxin123'
print(name.isalnum()) #字串由字母和數字組成
print(name.isalpha()) #字串由字母組成
print(name.isdigit()) #字串由數字組成
'''