Python的數字與字符


1、單行注釋:ctrl + ?        多行注釋:ctrl + /

2、整形:int

# 當前數字的二進制,至少用n位表示
n = input("請輸入整數:")
c = int(n) #把字符串轉化成數字
b = c.bit_length()
print(b)

3、字符串的魔法

  3.1、capitalize()---首字母大寫

n = “wuzhijie”
v =  n.capitalise()
print(v)

   3.2、casefold()和lower() -----所有變小寫,casefold更牛逼,很多未知的對相應變小寫

           upper()   ----------所有變大寫

           islower()和isupper()  ------判斷字符串是否全部為小寫或者大寫

test = "ALEX"
v1 = test.casefold()
print(v1)
v2 = test.lower()
print(v2)

------------

test = "Alex"
v1 = test.islower()
v2 = test.lower()
print(v1,v2)
v3 = test.isupper()
v4 = test.upper()
print(v3,v4)

  3.3、center(a,b)    ------設置寬度,並將內容居中;a為長度;b為空白位置填充的字符(可有可無,默認為空格符,只能輸入一個字符)

          ljust(a,b)    ------設置寬度,並將內容左對齊

          rjust(a,b)   ------設置寬度,並將內容右對齊

test = "abc"
v = test.center(20,"*")
print(v)

  3.4、count()   ---------字符串尋找子序列出現的次數

test = "aLexalexr"
v = test.count('ex',5,7)
print(v)

  3.5、endswith()和startswith()     ------判斷是否以什么結尾;判斷是否以什么開始

test = "alex"
v1 = test.endswith('ex')
v2 = test.startswith('la')
print(v1)
print(v2)

  3.6、find()和index()   ------從頭開始找,找到第一個之后獲取位置(注:find未找到返回數字:-1;index未找到會報錯)

test = "alexalex"
v1 = test.find('ex')
v2 = test.index('x')
print(v1)
print(v2)

  3.7、expandtabs()  -------把字符串中的 tab 符號('\t')轉為空格,tab 符號('\t')默認的空格數是8

test = "12345678\t9"
v = test.expandtabs(6)
print(v)
#可以制作表格
s = "a\tb\tc\n123\t321\t222\nqwe\tewq\taaa"
print(s.expandtabs(20))
 

3.8、len()   -------------返回對象(字符、列表、元組等)長度或項目個數。

test = "12345678\t9"
v = test.expandtabs(6)
print(v,len(v))

 3.9、format()和format_map()    ---------格式化,將一個字符串中的占位符替換為指定的值

#格式化,將一個字符串中的占位符替換為指定的值
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})
print(v1)
print(v2)

4.0、isalnum()   ---------判斷字符串中是否只包含 字母、數字和漢字

test = "1231#"
v = test.isalnum()
print(v)

 4.1、isalpha()  ---------判斷字符串中是否存在字母或者漢字

s = "阿薩a德"
a = s.isalpha()
print(a)

4.2、isdecimal()、isdigit()和isnumeric()  -----判斷當前輸入是否為阿拉伯數字,中間的能判斷多一種③,最后一個能判斷中文數字“三”

ceshi = ""
a1 = ceshi.isdecimal()
a2 = ceshi.isdigit()
a3 = ceshi.isnumeric() print(a1,a2,a3)

4.3、isidentifier()   -----判斷字符串中有字母、數字、下划線:標識符

ceshi = "_as111das"
a1 = ceshi.isidentifier()

print(a1)

4.4、isprintable() -------判斷字符串中是否存在不可顯示的字符

#\t  制表符
#\n 換行
ceshi = "as1a\t11d\nas" a1 = ceshi.isprintable() print(a1)

4.5、isspace()  --------判斷字符串中是否全部是空格鍵

ceshi = "as1a\t11d\nas"
a1 = ceshi.isprintable()
print(a1)

4.6、istitle()   ----判斷字符串中是否每一個單詞都首字母大寫

        title()     -----將字符串轉換成標題(即使每個單詞都首字母大寫)

ceshi = "ashd hsajkd jsahd  asjdj  asjd asd  aks"
a1 = ceshi.istitle()
print(a1)
a2 = ceshi.title()
print(a2)
a3 = a2.istitle()
print(a3)

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

ceshi = "對影成三人"
a1 = "*".join(ceshi)
print(a1)

4.8、strip()     ------默認移除字符串中的空白部分(空格、\t、\n);指定字符時,會移除指定字符(優先最多匹配字符)

        lstrip()    ------默認移除字符串中的左邊的空白部分;指定字符時,會移除指定字符

        rstrip()    ------默認移除字符串中的右邊的空白部分;指定字符時,會移除指定字符

test = "  Alexjhaf  "
test1 = "alexjhaf"
v1 = test.lstrip()
v2 = test.rstrip()
v3 = test.strip()
v4 = test1.strip("12ale33")
print(v1)
print(v2)
print(v3)
print(v4)

4.9、maketrans()和translate()

# test = "鋤禾日當午"
# test1 = "低頭思故鄉"

a = "鋤禾日當午,汗滴禾下土"
x = str.maketrans("鋤禾日當午","低頭思故鄉")
m = a.translate(x)
print(m)

5.0、partition()和rpartition()  ------以傳入參數進行分割,分割成三份(包含參數本身)。

        split(a,b)和rsplit(a,b)      ---------以傳入a參數進行分割,不傳入b參數,則以a參數個數進行分割;傳入b參數則以b個a參數進行分割(分割時,不包含a參數)

test = "asdajsakljsjma"
v1 = test.partition("s")
v2 = test.rpartition("s")
v3 = test.split("s",3)
v4 = test.rsplit("j",2)
print(v1)
print(v2)
print(v3)
print(v4)


a = “6+9”
v1,v2 = a.split("+")
v1 = int(v1) #6
v2 = int(v2) #9

5.1、splitlines()   ----根據換行符分割;參數(ture,false)是否保留換行符(默認不保留)

test = "asgfjasf\nasgfahsgf\nsahgfas"
v = test.splitlines()
print(v)

5.2、 swapcase()  -----大小寫轉換

test = "asdSAGDasba"
v = test.swapcase()
print(v)

 5.3、replace(a,b,c)  ------將字符串中的c個a參數替換成b參數;

test = "asakjgfuwyqfbasgfjakzxjhfgasakfjakjs"
v = test.replace("ak","12",3)
print(v)

 


免責聲明!

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



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