1、字符串
str = "nihaoshijie"
# 字符串转化成列表
list1 = list(str)
print(list1)
>>>["n","i","h","a","o","s","h","i","j","i","e"]
list2 = str.split("i")
print(list2)
>>>["n","haosh","j","e"]
#替换
newStr = str.replace("i", "n")
print(newStr)
>>>"nnhaoshnjne"
2、列表
list = ["ni","hao","shi","jie"]
# 列表转字符串
str1 = "".join(list)
print(str1)
>>>"nihaoshijie"
str2 = " ".join(list)
print(str2)
>>>"ni hao shi jie"
str3 = "."join(list)
print(str3)
>>>"ni.hao.shi.jie"
# 获取列表元素
first = list[0]
print(first)
>>>"ni"
second = list[1]
print(second)
>>>"hao"
# 列表转元组
tuple = tuple(list)
print(tuple)
>>>("ni","hao","shi","jie")
3、字典
dict = {"key1":"value1","key2":"value2","key3":"value3"}
# 根据key取value
value1 = dict.get("key1")
网友评论