字符操作总结补充

#String 操作

# a="Let's go "
# print(a)
# 1 * 重复输出字符串
# print('hello'*20)

# 2 [] ,[:] 通过索引获取字符串中字符,这里和列表的切片操作是相同的,具体内容见列表
# print('helloworld'[2:])

#关键字 in
# print(123 in [23,45,123])
# print('e2l' in 'hello')

# 4 % 格式字符串
# print('alex is a good teacher')
# print('%s is a good teacher'%'alex')

#5字符连接
# a='123'
# b='abc'
# d='44'
# # # c=a+b
# # # print(c)
# #
# c= ''.join([a,b,d])
# print(c)

 

# String的内置方法

# st='hello kitty {name} is {age}'
#

  1. # print(st.count('l')) # 统计元素个数
  2. # print(st.capitalize()) # 首字母大写
  3. # print(st.center(50,'#')) # 居中
  4. # print(st.endswith('tty3')) # 判断是否以某个内容结尾
  5. # print(st.startswith('he')) # 判断是否以某个内容开头
  6. # print(st.expandtabs(tabsize=20))#设定tab键的空格
  7. # print(st.find('t')) # 查找到第一个元素,并将索引值返回,查询不到不会报错
  8. # print(st.format(name='alex',age=37)) # 格式化输出的另一种方式 待定:?:{}
  9. # print(st.format_map({'name':'alex','age':22}))#格式化输出
  10. # print(st.index('t'))#查询t字符,如果查询不到就报错
  11. # print('asd'.isalnum())#判断是不是包含数字和字母和汉字。特殊字符不行
  12. # print('12632178'.isdecimal())#判断是否是十进制的数
  13. # print('1269999.uuuu'.isnumeric())#判断是不是数字
  14. # print('1269999.uuuu'.isdigit())#判断是否是个整数字。浮点数不行
  15. # print('abc'.isidentifier())#判断是否非法字符,例如变量名
  16. # print('Abc'.islower())#判断是不是小写,全小写
  17. # print('ABC'.isupper())#判断是不是全大写
  18. # print(' e'.isspace())#判断是不是空格
  19. # print('My title'.istitle())#判断标题,每个单词的首字母大写
  20. # print('My tLtle'.lower())#转小写
  21. # print('My tLtle'.upper())#转大写
  22. # print('My tLtle'.swapcase())#大写转小写,小写转大写
  23. # print('My tLtle'.ljust(50,'*'))#靠左字符*补充在右边填充到50个
  24. # print('My tLtle'.rjust(50,'*'))#靠右
  25. # print('\tMy tLtle\n'.strip())#把左右的换行符和空格去掉
  26. # print('\tMy tLtle\n'.lstrip())#去除左边
  27. # print('\tMy tLtle\n'.rstrip())#去除右边
  28. # print('ok')
  29. # print('My title title'.replace('itle','lesson',1))#替换(替换的对象,替换的内容,替换的次数)不加次数则全部替换
  30. # print('My title title'.rfind('t'))#从右边开始寻找
  31. # print('My title title'.split(' '))#以空格作为分割,然后存进列表["My","title","title"]
  32. # print('My title title'.rsplit('i',1))#只分割1次,以右为准
  33. # print('My title title'.split('i',1))#只分割1次
  34. # print('My title title'.title())#首字母大写
  35. #摘一些重要的字符串方法
  36. #1 print(st.count('l'))
  37. # print(st.center(50,'#')) # 居中
  38. # print(st.startswith('he')) # 判断是否以某个内容开头
  39. # print(st.find('t'))
  40. # print(st.format(name='alex',age=37)) # 格式化输出的另一种方式 待定:?:{}
  41. # print('My tLtle'.lower())
  42. # print('My tLtle'.upper())
  43. # print('\tMy tLtle\n'.strip())
  44. # print('My title title'.replace('itle','lesson',1))
  45. # print('My title title'.split('i',1))

猜你喜欢

转载自www.cnblogs.com/dyj559/p/10805502.html