python去除字符串中的换行,空格

import re
word = """关关雎鸠,在河之洲。
窈窕淑女,君子 好逑。
参差荇菜,左右     流之。
窈窕淑女,寤寐求之。
求之不得,寤寐思服。
悠哉悠哉,辗转 反侧。
参差荇菜,左右采之。
窈窕淑女,琴瑟友之。
参差荇菜,左右       芼之。
窈窕淑女,钟鼓乐之。"""
new_word = "".join((re.sub("\n", " ", word)).split(" "))

print(new_word)
  • 列表转字符串
demo = ["1",2,3,4,"5"]

def list_str(list):
    s_word = ""
    if len(list) != 0:
        while True:
            if  isinstance(list[0],int):
                s_word+=str(list[0])
                list.remove(list[0])
            else:
                s_word += list[0]
                list.remove(list[0])
            if len(list) == 0:
                break
    else:
        return "该列表为空"
    return s_word

print(list_str(demo))
  • 字符串的replace 方法:
ret= demo.replace(" ","")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值