Python老男孩 day10

來源:互聯網
上載者:User

標籤:出現   XA   with   數字   center   二進位   填充   格式化   test   

1、運算子
結果是值
算數運算
a = 10 * 10
賦值運算
a = a + 1 a+=1

結果是布爾值
比較運算
a = 1 > 5
邏輯運算
a = 1>6 or 1==1
成員運算
a = "蚊" in "鄭建文"

 

2、基礎資料型別 (Elementary Data Type)

Python2裡分 int 和 long

Python3裡只有 int

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

- int
將字串轉換為數字
a = "123"
print(type(a),a)

b = int(a)
print(type(b),b)

num = "0011"
v = int(num, base=2)     將num當做二進位輸出對應十進位
print(v)


- bit_lenght
 當前數位二進位,至少用n位表示
r = age.bit_length()

age = 5
# 1 1
# 2 10
# 3 11
# 4 100
# 5 101

print(r)

 

#首字母大寫

test = "aLex"
v = test.capitalize()
print(v)

 

#所有變小寫,casefold更牛逼,很多未知的對相應變小寫
v1 = test.casefold()
print(v1)
v2 = test.lower()
print(v2)

 

#設定寬度,並將內容置中
20 代指總長度
空白未知填充,一個字元,可有可無
v = test.center(20,"中")
print(v)

 

#去字串中尋找,尋找子序列的出現次數
test = "aLexalexr"
v = test.count(‘ex‘)
print(v)

test = "aLexalexr"
v = test.count(‘ex‘,5,6)    

5,6表示字串的開始和結束座標
print(v)

 

# 以什麼什麼結尾
# 以什麼什麼開始
test = "alex"
v = test.endswith(‘ex‘)
v = test.startswith(‘ex‘)
print(v)

 

#從開始往後找,找到第一個之後,擷取其位置
> 或 >=
test = "alexalex"
# 未找到 -1
v = test.find(‘ex‘)
print(v)

 

# index找不到,報錯 忽略
test = "alexalex"
v = test.index(‘8‘)
print(v)


# 格式化,將一個字串中的預留位置替換為指定的值
test = ‘i am {name}, age {a}‘
print(test)
v = test.format(name=‘alex‘,a=19)
print(v)

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

 

# 格式化,傳入的值 {"name": ‘alex‘, "a": 19}
test = ‘i am {name}, age {a}‘
v1 = test.format(name=‘df‘,a=10)
v2 = test.format_map({"name": ‘alex‘, "a": 19})

上兩句效果相同

 

# 字串中是否只包含 字母和數字
test = "123"
v = test.isalnum()
print(v)

 

Python老男孩 day10

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.