python入门容器-列表与字符串转化Day05

列表转换为字符串:

result = "连接符".join(列表)

# 应用
# 需求:根据某个逻辑循环拼接字符串
#       0-9
# result = ""
# for number in range(10):
#     # 缺点:每次循环都会产生新字符串,产生垃圾
#     result += str(number)
# print(result)  # 0123456789

# 解决:将不可变数据改为可变数据
result = []
for number in range(10):
    result.append(str(number))
result = "".join(result)
print(result)  # 0123456789

字符串转换为列表:

列表 = “a-b-c-d”.split(“分隔符”)

"""
    字符串转换为列表:
    列表 = “a-b-c-d”.split(“分隔符”)
"""
result = "a/b/c/d".split("/")
print(result)

# 应用
# 一个字符串记录多个信息
result = "唐僧_孙悟空_八戒".split("_")
print(result)

 

"""
    练习:将下列英文语句按照单词进行翻转.
    转换前:To have a government that is of people by people for people
    转换后:people for people by people of is that government a have To
"""
message = "To have a government that is of people by people for people"
list_temp = message.split(" ")
message = " ".join(list_temp[::-1])
print(message)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值