1.len(返回字符串的长度)
:
text = "Hello, world!"
print(len(text)) # Output: 13
2.find(查找字符串在某字符串中是否包含)
:
text = "Hello, world!"
print(text.find("world")) # Output: 7
3.index(查找字符串在某字符串中是否包含)
:
text = "Hello, world!"
print(text.index("world")) # Output: 7
4.count(统计字符串中出现的次数)
:
text = "Hello, world!"
print(text.count("l")) # Output: 3
5.replace(替换字符串中的字符)
:
text = "Hello, world!"
new_text = text.replace("world", "Python")
print(new_text) # Output: Hello, Python!
6.split(分割字符串)
:
text = "Hello, world!"
words = text.split(", ")
print(words) # Output: ['Hello', 'world!']
7.join(多个字符串合并为一个字符串)
:
words = ['Hello', 'world!']
text = ", ".join(words)
print(text) # Output: Hello, world!
8.capitalize(讲字符串的第一个字符转为大写)
:
text = "hello, world!"
print(text.capitalize()) # Output: Hello, world!
9.title(每个单词首字母大写)
:
text = "hello, world!"
print(text.title()) # Output: Hello, World!
10.startswith(检查开头字符串)
:
text = "Hello, world!"
print(text.startswith("Hello")) # Output: True
11.endswith(检查尾部为某字符串)
:
text = "Hello, world!"
print(text.endswith("world!")) # Output: True
12.lower(大写字符串转为小写)
:
text = "Hello, world!"
print(text.lower()) # Output: hello, world!
13.upper(小写转为大写)
:
text = "Hello, world!"
print(text.upper()) # Output: HELLO, WORLD!
14.lstrip(删除左侧空格)
:
text = " Hello, world!"
print(text.lstrip()) # Output: Hello, world!
15.rstrip(删除右侧空格)
:
text = "Hello, world! "
print(text.rstrip()) # Output: Hello, world!
16.strip(删除两侧空格)
:
text = " Hello, world! "
print(text.strip()) # Output: Hello, world!
17.partition(按照某个字符串分割成三部分)
:
text = "Hello, world!"
parts = text.partition(", ")
print(parts) # Output: ('Hello', ', ', 'world!')
18.splitlines(按照行分割)
:
text = "Hello,\nworld!"
lines = text.splitlines()
print(lines) # Output: ['Hello,', 'world!']
19.isalpha(判断字符串组成是否全部为字母)
:
text = "Hello"
print(text.isalpha()) # Output: True
20.isalnum(判断字符串中是否只包含字母或者数字)
:
text = "Hello123"
print(text.isalnum()) # Output: True
21.isspace(判断字符串中是否只含有空格)
:
text = " "
print(text.isspace()) # Output: True
22.python双向链表的使用
在Python中,您可以使用 collections.deque
模块来实现双向链表。collections.deque
是一个双端队列,支持从两端快速地增加和删除元素,因此非常适合用作双向链表的实现。
from collections import deque# 创建一个空的双向链表
dll = deque()# 在链表的末尾添加元素
dll.append('a')
dll.append('b')
dll.append('c')
print("双向链表:", dll)# 在链表的开头添加元素
dll.appendleft('x')
dll.appendleft('y')
dll.appendleft('z')
print("双向链表:", dll)# 从链表的末尾移除元素
dll.pop()
print("双向链表:", dll)# 从链表的开头移除元素
dll.popleft()
print("双向链表:", dll)
2022蓝桥杯大学B组python题:消除游戏
解题代码:
N=10**6+10
pos=[]
l,r=[0]*N,[0]*N
st=[False]*N
s=input()
n=len(s)
s="@"+s+"@"
# 构建双向链表
for i in range(1,n+1):l[i]=i-1r[i]=i+1
# 查找所有边缘字符
def check(i):if s[l[i]]=="@" or s[r[i]]=="@":returnif s[l[i]]==s[i] and s[r[i]]!=s[i]:pos.append(r[i])pos.append(i)if s[l[i]]!=s[i] and s[r[i]]==s[i]:pos.append(l[i])pos.append(i)
def remove(j):r[l[j]]=r[j]l[r[j]]=l[j]# 删除j结点,置为Truest[j]=True
for i in range(1,n+1):check(i)
while pos:ne=[]for p in pos:if st[p]:continueremove(p)ne.append(l[p])ne.append(r[p])pos=[]for e in ne:if not st[e]:check(e)
ans=""
for i in range(1,n+1):if not st[i]:ans+=s[i]
if ans:print(ans)
else:print("EMPTY")