当前位置:   article > 正文

Python字符串操作

Python字符串操作

1.len(返回字符串的长度):

  1. text = "Hello, world!"
  2. print(len(text)) # Output: 13

2.find(查找字符串在某字符串中是否包含):

  1. text = "Hello, world!"
  2. print(text.find("world")) # Output: 7

3.index(查找字符串在某字符串中是否包含):

  1. text = "Hello, world!"
  2. print(text.index("world")) # Output: 7

4.count(统计字符串中出现的次数):

  1. text = "Hello, world!"
  2. print(text.count("l")) # Output: 3

5.replace(替换字符串中的字符):

  1. text = "Hello, world!"
  2. new_text = text.replace("world", "Python")
  3. print(new_text) # Output: Hello, Python!

6.split(分割字符串):

  1. text = "Hello, world!"
  2. words = text.split(", ")
  3. print(words) # Output: ['Hello', 'world!']

7.join(多个字符串合并为一个字符串):

  1. words = ['Hello', 'world!']
  2. text = ", ".join(words)
  3. print(text) # Output: Hello, world!

8.capitalize(讲字符串的第一个字符转为大写):

  1. text = "hello, world!"
  2. print(text.capitalize()) # Output: Hello, world!

9.title(每个单词首字母大写):

  1. text = "hello, world!"
  2. print(text.title()) # Output: Hello, World!

10.startswith(检查开头字符串):

  1. text = "Hello, world!"
  2. print(text.startswith("Hello")) # Output: True

11.endswith(检查尾部为某字符串):

  1. text = "Hello, world!"
  2. print(text.endswith("world!")) # Output: True

12.lower(大写字符串转为小写):

  1. text = "Hello, world!"
  2. print(text.lower()) # Output: hello, world!

13.upper(小写转为大写):

  1. text = "Hello, world!"
  2. print(text.upper()) # Output: HELLO, WORLD!

14.lstrip(删除左侧空格):

  1. text = " Hello, world!"
  2. print(text.lstrip()) # Output: Hello, world!

15.rstrip(删除右侧空格):

  1. text = "Hello, world! "
  2. print(text.rstrip()) # Output: Hello, world!

16.strip(删除两侧空格):

  1. text = " Hello, world! "
  2. print(text.strip()) # Output: Hello, world!

17.partition(按照某个字符串分割成三部分):

  1. text = "Hello, world!"
  2. parts = text.partition(", ")
  3. print(parts) # Output: ('Hello', ', ', 'world!')

18.splitlines(按照行分割):

  1. text = "Hello,\nworld!"
  2. lines = text.splitlines()
  3. print(lines) # Output: ['Hello,', 'world!']

19.isalpha(判断字符串组成是否全部为字母):

  1. text = "Hello"
  2. print(text.isalpha()) # Output: True

20.isalnum(判断字符串中是否只包含字母或者数字):

  1. text = "Hello123"
  2. print(text.isalnum()) # Output: True

21.isspace(判断字符串中是否只含有空格):

  1. text = " "
  2. print(text.isspace()) # Output: True

22.python双向链表的使用

在Python中,您可以使用 collections.deque 模块来实现双向链表。collections.deque 是一个双端队列,支持从两端快速地增加和删除元素,因此非常适合用作双向链表的实现。

  1. from collections import deque
  2. # 创建一个空的双向链表
  3. dll = deque()
  4. # 在链表的末尾添加元素
  5. dll.append('a')
  6. dll.append('b')
  7. dll.append('c')
  8. print("双向链表:", dll)
  9. # 在链表的开头添加元素
  10. dll.appendleft('x')
  11. dll.appendleft('y')
  12. dll.appendleft('z')
  13. print("双向链表:", dll)
  14. # 从链表的末尾移除元素
  15. dll.pop()
  16. print("双向链表:", dll)
  17. # 从链表的开头移除元素
  18. dll.popleft()
  19. print("双向链表:", dll)

2022蓝桥杯大学B组python题:消除游戏

解题代码:

  1. N=10**6+10
  2. pos=[]
  3. l,r=[0]*N,[0]*N
  4. st=[False]*N
  5. s=input()
  6. n=len(s)
  7. s="@"+s+"@"
  8. # 构建双向链表
  9. for i in range(1,n+1):
  10. l[i]=i-1
  11. r[i]=i+1
  12. # 查找所有边缘字符
  13. def check(i):
  14. if s[l[i]]=="@" or s[r[i]]=="@":
  15. return
  16. if s[l[i]]==s[i] and s[r[i]]!=s[i]:
  17. pos.append(r[i])
  18. pos.append(i)
  19. if s[l[i]]!=s[i] and s[r[i]]==s[i]:
  20. pos.append(l[i])
  21. pos.append(i)
  22. def remove(j):
  23. r[l[j]]=r[j]
  24. l[r[j]]=l[j]
  25. # 删除j结点,置为True
  26. st[j]=True
  27. for i in range(1,n+1):
  28. check(i)
  29. while pos:
  30. ne=[]
  31. for p in pos:
  32. if st[p]:continue
  33. remove(p)
  34. ne.append(l[p])
  35. ne.append(r[p])
  36. pos=[]
  37. for e in ne:
  38. if not st[e]:
  39. check(e)
  40. ans=""
  41. for i in range(1,n+1):
  42. if not st[i]:
  43. ans+=s[i]
  44. if ans:
  45. print(ans)
  46. else:
  47. print("EMPTY")

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/352615
推荐阅读
相关标签
  

闽ICP备14008679号