当前位置:   article > 正文

python例题(10)_s1=input().strip() c=input().strip() s2=s1.replace

s1=input().strip() c=input().strip() s2=s1.replace(c,"") print(s2) count= le

1.issuperset():测试是否为父集

  1. a = {1, 2, 3, 4, 5, 6, 7}
  2. b = {1, 2, 3, 4}
  3. print(a.issuperset(b))
  4. # True

2.update():将一个集合加到另一个集合中

  1. a = {1, 3, 5, 7}
  2. b = {2, 4, 6, 8}
  3. a.update(b)
  4. print(a)
  5. # {1, 2, 3, 4, 5, 6, 7, 8}

3.difference_update():删除集合中重复元素

  1. a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
  2. b = {1, 3, 5, 7, 9}
  3. a.difference_update(b)
  4. print(a)
  5. # {0, 2, 4, 6, 8}
  6. 覆盖式操作,原有值会被盖掉

4.冻结集合:frozenset是不可变的数据类型集合称为冻结集合,定义好内容后不可修改、删除、操作元素

  1. x = frozenset([1, 2, 3])
  2. y = frozenset([4, 5, 6])
  3. print(x, y)
  4. print(x & y, x | y)
  5. # frozenset({1, 2, 3}) frozenset({4, 5, 6})
  6. # frozenset() frozenset({1, 2, 3, 4, 5, 6})

5.字符串常量:字符串是由单引号和双引号包裹起来的任意不可变文本序列。

  1. s1 = 'china' # 字符串常量
  2. print(s1[0]) # 可通过内置容器的索引方式来遍历元素 c
  3. # s1[1]='h' # 报错,字符串常量是一个不可变的序列
  4. s2 = ['china', 'hello', 'world'] # 字符串列表

6.拼接字符串。符号:+

  1. s1 = '我存款金额为:'
  2. num = -100
  3. print(s1 + str(num))
  4. # PS:字符串的拼接对象只能是字符串

7.多行字符串:字符串长度多于一行,使用三引号将字符串包夹

  1. s1 = '''china china china
  2. china china china
  3. china china china'''
  4. print(s1)
  5. # china china china
  6. # china china china
  7. # china china china

8.字符串的切片。格式:strname[start,end,step]

  1. s1 = '2022.加油!中国!'
  2. print(s1) # 2022.加油!中国!
  3. print(s1[0]) # 2
  4. print(s1[0:]) # 2022.加油!中国!
  5. print(s1[5:]) # 加油!中国!
  6. print(s1[:4]) # 2022
  7. print(s1[1:11:2]) # 02加!国
  8. print(s1[-1:-4:-1]) # !国中

9.输入员工身份证号,输出出生日期和性别

  1. s1 = input('请输入您的身份证号:')
  2. print('您的身份证号是:', s1)
  3. if int(s1[16]) % 2 == 0:
  4. print('性别:女')
  5. else:
  6. print('性别:男')
  7. print('出生日期为:' + s1[6:10] + '年' + s1[10:12] + '月' + s1[12:14] + '日')
  8. # 请输入您的身份证号:1233456200203182343
  9. # 您的身份证号是: 1233456200203182343
  10. # 性别:男
  11. # 出生日期为:6200年20月31日

10.分隔合并字符串。分隔格式:strname.split(sep,maxsplit)
合并格式:strnew=string.join(iterable)

  1. # 分隔
  2. s1 = 'Python 软件官网 >>> https://www.python.org/'
  3. print('原串:', s1)
  4. l1 = s1.split()
  5. l2 = s1.split('>>>')
  6. l3 = s1.split('.')
  7. l4 = s1.split(" ", 3)
  8. print(l1, l2, l3, l4, sep='\n')
  9. # 原串: Python 软件官网 >>> https://www.python.org/
  10. # ['Python', '软件官网', '>>>', 'https://www.python.org/']
  11. # ['Python 软件官网 ', ' https://www.python.org/']
  12. # ['Python 软件官网 >>> https://www', 'python', 'org/']
  13. # ['Python', '软件官网', '>>>', 'https://www.python.org/']
  14. # 合并
  15. s1 = 'fsegfgsdggrdgs'
  16. s3 = '.'.join(s1)
  17. print(s3)
  18. # f.s.e.g.f.g.s.d.g.g.r.d.g.s

11.count()方法:检索字符串在另一个串中出现的次数,若不存在则返回0
格式:strname.count(substring---要检索的子串的名称)

  1. s1 = '****ABC***DEF****'
  2. c1 = s1.count('*')
  3. c2 = s1.count('?')
  4. print('次数:', c1, c2)
  5. # 次数: 11 0

12.find()方法:检索是否包含指定字符串,不存在返回-1,否则返回出现的索引值
格式:strname.find(sub)

  1. s1 = input('主串:')
  2. s2 = input('子串:')
  3. n = s1.find(s2)
  4. if n > -1:
  5. print('存在,第一次出现索引为:', n)
  6. else:
  7. print('不存在')
  8. # 主串:23456AS
  9. # 子串:AS
  10. # 存在,第一次出现索引为: 5
  11. # PS:一般用于在主串中查找子串是否存在

13.大小写互换:strname.swapcase()。大写—>小写:strname.lower()。小写—>大写:strname.upper()

  1. s1 = input()
  2. print(s1.swapcase())
  3. print(s1.upper())
  4. print(s1.lower())
  5. # sdfgaetASFEF
  6. # SDFGAETasfef
  7. # SDFGAETASFEF
  8. # sdfgaetasfef

14.判断字符串中字母出现的次数。格式:strname.isalpha()

  1. s1 = input('请输入:')
  2. c = {} # 定义空字典
  3. for i in s1:
  4. if i.isalpha() == 1:
  5. c[i] = c.get(i, 0) + 1 # 计算i的字典的键值
  6. print(c)
  7. # 请输入:awsedrfgt
  8. # {'a': 1, 'w': 1, 's': 1, 'e': 1, 'd': 1, 'r': 1, 'f': 1, 'g': 1, 't': 1}

15.字符串删除。统计字符串中的单词个数

  1. s1 = input('请输入单词:')
  2. s1 = s1.strip() # 删除左右两侧空格
  3. cnt = 1
  4. print(s1)
  5. i = 0
  6. while i < len(s1) - 1:
  7. if s1[i] != ' ' and s1[i + 1] == ' ':
  8. cnt += 1
  9. i += 1
  10. if s1 == ' ':
  11. cnt = 0
  12. print('单词个数:', cnt)
  13. # 请输入单词:napkin system mmm
  14. # napkin system mmm
  15. # 单词个数: 3

16.strname.replace(‘需替换字符串’,’替换结果’)

  1. s1 = '****ABC***123****'
  2. print(s1.replace('*', '?'))
  3. # ????ABC???123????

17.字符串文本对齐。Center:居中。ljust:左对齐。rjust:右对齐

  1. poem='静夜思','李白','窗前明月光','疑是地上霜','举头望明月','低头思故乡'
  2. for line in poem:
  3. print('%s'%line.center(10)) # 居中,指定字符宽度
  4. for line in poem:
  5. print('%s'%line.rjust(10)) # 右对齐
  6. for line in poem:
  7. print('%s'%line.ljust(10)) # 左对齐

18.编写程序,检测输入的字符串密码是否是一个合法的密码。规则如下:
密码至少8个字符;只能包含英文和数字;密码至少包含2个数字

  1. str1 = input('请输入您的密码')
  2. t = 0
  3. for i in str1:
  4. if i.isdigit(): # 判断字符串是否为数字,计算数字个数
  5. t += 1
  6. if len(str1) >= 8:
  7. if str1.isalnum(): # 判断字符串是否只包含字母和数字
  8. if t >= 2:
  9. print('密码设置成功')
  10. else:
  11. print('密码输出数字少于两个')
  12. else:
  13. print('密码只能包含字母和数字')
  14. else:
  15. print('密码至少要有8个字符')
  16. # 请输入您的密码517318tqwei
  17. # 密码设置成功

19.编写程序,实现一个二进制数转为十进制数,如:1001的十进制为9,即执行执行1*2^0+0*2^1+0*2^2+1*2^3

  1. str1 = input('请输入二进制:')
  2. d1 = 0 # 接收转换结果
  3. t = 0 # 权值
  4. f = 1
  5. if str1.isnumeric(): # 判断字符串中至少有一个字符且所有字符均为数字
  6. str2 = str1[::-1] # 对字符串进行切片,而后逆置,str2存储逆序的str1
  7. for i in str2:
  8. if i == '0' or i == '1':
  9. d1 = d1 + int(i) * 2 ** t
  10. else:
  11. print('无效数字')
  12. f = 0
  13. break
  14. t += 1 # 权值加1
  15. if f:
  16. print(str1, '转为十进制整数是:', d1)
  17. # 请输入二进制:11111111
  18. # 11111111 转为十进制整数是: 255

20.国际标准书号共10位:d1d2d3d4d5d6d7d8d9d10,最后一位d10是校验位,校验位计算公式为:(d1*1+d2*2+d3*3+d4*4+d5*5+d6*6+d7*7+d9*9)%11,若d10计算结果为10则使用x表示。编写程序,输入前9个,输出标准书号。

  1. str1 = input('请输入国际标准书号前9位:')
  2. check = 0
  3. t = 0
  4. for i in str1:
  5. check = check + int(i) * t
  6. t += 1
  7. check %= 11
  8. if check == 10:
  9. check = 'x'
  10. print(str1 + str(check))
  11. # 请输入国际标准书号前9位:987654321
  12. # 987654321x
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/563881
推荐阅读
相关标签
  

闽ICP备14008679号