int及str方法的使用


python 類型有:整形int,字符串str,列表list,元祖tuple,字典dict,布爾值bool
a ='10'
print(type(a),a)
b=int (a) #將字符串轉為int類型,使用type可以查看類型
print(type(b),b)

str:
1.count() 去字符串中尋找,尋找子序列的出現次數,如下
name="root"
text=name.count("r")
print(text) 結果:1

2.capitalize() 首字母大寫
name ="root"
text=name.capitalize()
print(text) 輸出結果為Root
3.casefold() 所有字母小寫
name ="ROOT"
text=name.casefold()
print(text) 輸出結果為root

4.center()內容居中
name ="ROOT"
text=name.center(10,'"')# 10代表總長度
print(text) 輸出結果為:"""ROOT"""

5.startswith以什么開始,endswith已什么結束,返回為true或flase
name ="ROOT"
text=name.startswith("T")
text1=name.endswith("T")
print(text) #輸出結果為flase
print(text1)#輸出結果為true

6.*****find 從開始往后找,找到第一個之后,獲取其未知

 test = "alexalex"# 未找到 -1
 v = test.find('ex')
 print(v) #輸出結果為2

7.****format 格式化,將一個字符串中的占位符替換為指定的值
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)
# expandtabs(),斷句20,
test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123"
v = test.expandtabs(20)
print(v) #結果以表格形式輸出

# 判斷是否是字母,漢字
test = "as2df"
v = test.isalpha()
print(v) 輸入false

#當前輸入是否是數字
test = "二" # 1,②
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3) 輸出False False True

# 是否存在不可顯示的字符
# \t 制表符
# \n 換行
test = "oiuas\tdfkj"
v = test.isprintable()
print(v)輸出false
# 判斷是否全部是空格
test = " "
v = test.isspace()
print(v) 輸出true

#  判斷是否是標題
test = "Return True if all cased characters in S are uppercase and there is"
v1 = test.istitle()
print(v1) #輸出false
v2 = test.title()
print(v2) #將上面句子中所有首字母大寫,輸出結果:Return True If All Cased Characters In S Are Uppercase And There Is
v3 = v2.istitle()
print(v3) #輸出true

#  ***** 將字符串中的每一個元素按照指定分隔符進行拼接
test = "你是風兒我是沙"
# t = ' '
v = "_".join(test)
print(v) 輸出:你_是_風_兒_我_是_沙


# 18 判斷是否全部是大小寫 和 轉換為大小寫
test = "Alex"
v1 = test.islower()
v2 = test.lower()
print(v1, v2) 輸出:false alex
v1 = test.isupper()
v2 = test.upper()
print(v1,v2) 輸出:false AEX


# 去除左右空白
test="aa"
v = test.lstrip()
v1 = test.rstrip()
v2 = test.strip()
print(v,v1,v2)

#  分割為三部分
test = "testasdsddfg"
v = test.partition('s')
print(v) 輸出:('te', 's', 'tasdsddfg') 從左往右分割
v = test.rpartition('s')
print(v) 輸出:('testasd', 's', 'ddfg') 從右往左按s進行分割

# 22***** 分割為指定個數
test = "testasdsddfg"
v = test.split('s',2)
print(v) 輸出:['te', 'ta', 'dsddfg']
 


免責聲明!

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



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