【一】整型
1.1 - 十进制转二进制 bin()
num = 20
print(bin(num)) # 0b10100
1.2 - 十进制转八进制 oct()
num = 30
print(oct(num)) #0o36
1.3 - 十进制转十六进制 hex()
num = 40
print(hex(num)) #0x28
1.4 - 非进制转十进制:int()
hex1 = 0x28
print(int(hex1)) # 40
【二】浮点型
2.1 - 小数取整(四舍五入)
flat_round = 3.141526
print(round(flat_round)) # 3
【三】字符类型
3.1 - 字符串拼接
str1 = 'Hello,'
str2 = 'World!'
result_str = str1 + ' ' + str2
print(result_str) # 输出: Hello, World!
3.2 - 索引取值
3.2.1 正向取值
str1 = 'test'
print(str1[0]) # t
3.2.2 反向取值
str1 = 'test'
print(str1[-2]) # s
3.3 - 切片取值
text = 'Python'
substring = text[1:4] # 从索引1到索引4(不包括4)
print(substring) # yth
3.3.1 步长
text = 'Python'
res = text[0:4:2]
print(res) # Pt
3.3.2 步长反向取值
text = 'Python'
res = text[-1:-4:-2]
print(res) # nh
3.1 - 长度计算 len()
text = 'Python'
print(len(text)) # 6
3.2 - 存在检测 in, not in
test = 'happy'
res = 'p' in test
print(res) # True
info = 'SAD!'
res1 = 'SA1' not in info
print(res1) # True
3.3 - 去除首尾空格 strip()
test = ' out '.strip()
print(test) # out
3.3.1 去除首位指定字符 strip('指定字符')
str1 = 'test'
print(str1.strip('t')) # es
3.4 - 按指定字符分割 split
str1 = 'join-me'
print(str1.split('-')) # ['join', 'me']
3.3 - 将所有字母转换为大写 upper
str1 = 'python'
print(str1.upper()) # PYTHON
3.4 - 将所有字母转换为小写 lower
str1 = 'PYTHON'
print(str1.lower()) # python
3.5 - 判断是否以指定字符串开头 startswith
str1 = 'python is a high-level language'
print(str1.startswith('python')) # True
3.6 - 判断是否以指定字符串结尾 endswith
str1 = 'python is a high-level language'
print(str1.endswith('language')) # True
3.7 - 字符串拼接 join
# 从可迭代对象中取出多个字符串,然后按照指定的分隔符进行拼接,拼接的结果为字符串
str1 = 'python is a high-level language'
print('-'.join(str1)) # p-y-t-h-o-n- -i-s- -a- -h-i-g-h---l-e-v-e-l- -l-a-n-g-u-a-g-e
# 从列表中取出多个元素,然后按照指定字符作为分隔符号进行拼接
print('|'.join(['wx','alan','anna'])) # wx|alan|anna
3.8 - 用新的字符替换字符串中旧的字符 replace
str1 = 'python is a high-level language'
print(str1.replace('python','java')) # java is a high-level language
3.9 - 判断字符串是否为纯数字 isdigit
str1 = '12345'
str2 = '123,45'
print(str1.isdigit()) # True
print(str2.isdigit()) # False
3.10 - 输出从字符串左边起第一个指定字符的索引 find
str1 = 'java is a high-level language'
print(str1.find('g')) # 12
3.11 - 统计指定字符出现次数 count
str1 = 'java is a high-level language'
print(str1.count('g')) # 3
3.12 - 将字符串首字母转为大写 capitalize
str1 = 'python is a high-level language'
print(str1.capitalize()) # Python is a high-level language
3.13 - 将字符串内字母大小写转换 swapcase
str1 = 'python is a high-level language'
print(str1.swapcase()) # PYTHON IS A HIGH-LEVEL LANGUAGE
3.14 - 将字符串内所有单词首字母转换为大写 title
str1 = 'python is a high-level language'
print(str1.title()) # Python Is A High-Level Language
3.15 - 判断字符串属性 isxxxx
name = 'Wx'
print(name.isalnum()) # 字符串中既可以包含数字也可以包含字母,True
print(name.isalpha()) # 字符串中只包含字母,True
print(name.isidentifier()) # 字符串是否是合法标识符,True
print(name.islower()) # 字符串是否是纯小写,False
print(name.isupper()) # 字符串是否是纯大写,False
print(name.isspace()) # 字符串是否全是空格,False
print(name.istitle()) # 字符串中的单词首字母是否都是大写,True
【四】列表类型
4.1 - 在列表末尾添加元素 append
list1 = [1, 2, 3, 4, 5]
print(list1)
list1.append(6)
print(list1) # [1, 2, 3, 4, 5, 6]
PS: 每次仅能添加一个元素
4.2 - 在列表末尾一次添加多个元素 extend()
list1 = [1, 2, 3, 4, 5]
# more_num = [6, 7, 8]
more_num = ['Wx', 'anan']
list1.extend(more_num)
print(list1)
4.3 - 在指定位置添加元素 insert
list1 = [1, 2, 3, 4, 5]
list1.insert(0,'Wx')
print(list1)
4.4 - 删除指定索引的元素 del
list1 = [1, 2, 3, 4, 5]
del list1[4]
print(list1) # [1, 2, 3, 4]
4.5 - 删除列表最后一个元素且将删除的值返回 pop
list1 = [1, 2, 3, 4, 5]
list1.pop(0)
print(list1) # [2, 3, 4, 5]
list1 = [1, 2, 3, 4, 5]
list2 = list1.pop(0)
print(list2)
4.6 - 根据输入删除指定元素 remove
list1 = [1, 2, 3, 4, 5, 'wx']
list1.remove('wx')
print(list1) # [1, 2, 3, 4, 5]
4.7 - 颠倒列表内所有元素 reverse
list1 = [1, 2, 3, 4, 5, 'wx']
list1.reverse()
print(list1) # ['wx', 5, 4, 3, 2, 1]
4.8 - 对元素升序排序 sort
list1 = [7, 8, 3, 4, 5, 6, 1, 2]
list1.sort()
print(list1) # [1, 2, 3, 4, 5, 6, 7, 8]
# 降序输出
list1 = [7, 8, 3, 4, 5, 6, 1, 2]
list1.sort(reverse=True)
print(list1) # [8, 7, 6, 5, 4, 3, 2, 1]
标签:内置,language,Python,str1,list1,python,字符串,print,方法
From: https://www.cnblogs.com/wx-x/p/17868373.html