Python字符串操作

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-1
    r[i]=i+1
# 查找所有边缘字符
def check(i):
    if s[l[i]]=="@" or s[r[i]]=="@":
        return
    if 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结点,置为True
    st[j]=True
for i in range(1,n+1):
    check(i)
while pos:
    ne=[]
    for p in pos:
        if st[p]:continue
        remove(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")

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

马龙强_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值