字符串拼接 - 连接多个字符串。
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name)
字符串分割 - 使用分隔符或空格分割字符串。
text = "apple,banana,cherry"
words = text.split(',')
print(words)
大小写转换 - 转换字符串的大小写。
message = "Python is Awesome!"
print(message.lower())
print(message.upper())
字符串替换 - 替换字符串中的子串。
greeting = "Hello World!"
new_greeting = greeting.replace("World", "Python")
print(new_greeting)
正则表达式匹配 - 使用re
模块进行模式匹配。
以下方法可以检查给定列表是不是存在重复元素,它会使用 set() 函数来移除所有重复元素。
def all_unique(lst):
return len(lst)== len(set(lst))
x = [1,1,2,2,3,2,3,4,5,6]
y = [1,2,3,4,5]
all_unique(x) # False
all_unique(y) # True
2字符元素组成判定
检查两个字符串的组成元素是不是一样的。
from collect