1. 程式人生 > >Python自動化之列表

Python自動化之列表

換行 ndt 識別 isa decimal als 剔除 判斷 digi

##將首字母改成大寫
# name = ‘alex‘
# v = name.capitalize()
# print(v)

##將首字母改成小寫
# name = ‘Alex‘
# v = name.casefold()
# print(v)

###傳一個值,兩邊補的是添加的字符
# name = ‘Alex‘
# v = name.center(20,‘*‘)
# print(v)

##統計字符串出現的次數
# name = ‘Alex‘
# v = name.count(‘A‘)
# print(v)


##判斷字符的結尾是否以某個字符串結尾的
# name = ‘Alex‘
# v = name.endswith(‘ex‘)
# print(v)


##制表符(包含前邊的值,整體占8個)
# name = ‘Alex\tegon\twupeiqi‘
# v = name.expandtabs(8)
# print(v)

##索引
# name = ‘Alex‘
# v = name.find(‘A‘)
# print(v)

##字符串格式化
# tpl = ‘我是:{0};年齡:{1};性別:{2}‘
# v = tpl.format(‘李傑‘,‘12‘,‘女‘)
# print(v)

# tpl = ‘我是:{name};年齡:{age};性別:{sex}‘
# v = tpl.format(name = ‘李傑‘,age = 12,sex = ‘女‘)
# print(v)


##根據鍵找到對應位置的值
# tpl = ‘我是:{name};年齡:{age};性別:{sex}‘
# v = tpl.format_map({‘name‘:‘李傑‘,‘age‘:‘12‘,‘sex‘:‘女‘})
# print(v)


##找到對應字符串的索引
# name = ‘Alex‘
# v = name.index(‘A‘)
# print(v)

#判斷
# name = ‘Alex‘
# v = name.isalnum()#只能包含字母、數字、漢字
# print(v)

##判斷是否全部為字母
# name = ‘Alex‘
# v = name.isalpha()
# print(v)

##判斷是否全部為阿拉伯數字
# my_num = ‘123‘
# v = my_num.isdecimal()
# print(v)

##能識別阿拉伯數字②
# my_num = ‘123②‘
# v = my_num.isdigit()
# print(v)

##能識別多種字符
# my_num = ‘123②二‘
# v = my_num.isnumeric()
# print(v)


##判斷是否為關鍵字
# my_num = ‘num‘
# v = my_num.isidentifier()
# print(v)

##判斷是否都為小寫
# name = ‘alex‘
# v = name.islower()
# print(v)

##在字符串中出現的打印不出來的為False
# name = ‘al\nex‘
# v = name.isprintable()
# print(v)

##判斷是否全部為空格
# name = ‘al ex‘
# v = name .isspace()
# print(v)

##判斷是否全部為大寫
# name = ‘alex‘
# v = name.isupper()
# print(v)

#內部循環每一個元素
# name = ‘alex‘
# v = ‘*‘.join(name)
# print(v)


##把值放到左邊,右邊填充相應剩下長度的字符
# name = ‘alex‘
# v = name.ljust(20,‘*‘)
# print(v)


##不僅可以移除空白,也可以移除換行符,制表符也可以自定義
# name = ‘alex‘
# v = name.strip(‘x‘)
# print(v)


##將字符串的大小寫互換
# name = ‘Alex‘
# v = name.swapcase()
# print(v)

##替換字符串
# name = ‘你love他‘
# v = name.replace(‘love‘,‘愛‘)
# print(v)

##分割,將分割後的元素剔除
# name = ‘你love他‘
# v = name.split(‘love‘)
# print(v)

Python自動化之列表