zoukankan      html  css  js  c++  java
  • python 中去除空格的方法

    python 中去除空格的方法:

    def trim(s):
        l=[]
        for i in s:
            if i!=' ':
                l.append(i)
        return ''.join(l)

    其中可以使用下面的

    ''.join(list) 

    ','.join(list) 

    python 去除收尾空格的方法:

    def trim(s):
        while len(s)!=0:
            if s[0]==' ':
                s=s[1:]
            elif s[-1]==' ':
                s=s[:-1]
            else:
                break
        return s
    
    # 测试:
    if trim('hello  ') != 'hello':
        print('测试失败!')
    elif trim('  hello') != 'hello':
        print('测试失败!')
    elif trim('  hello  ') != 'hello':
        print('测试失败!')
    elif trim('  hello  world  ') != 'hello  world':
        print('测试失败!')
    elif trim('') != '':
        print('测试失败!')
    elif trim('    ') != '':
        print('测试失败!')
    else:
        print('测试成功!')

    运行过代码没有问题

  • 相关阅读:
    剑指 Offer 05. 替换空格
    SNGAN
    CycleGAN
    Robust Pre-Training by Adversarial Contrastive Learning
    FineGAN
    TGAN
    SRGAN
    A Tutorial on Energy-Based Learning
    CoGAN
    EBGAN
  • 原文地址:https://www.cnblogs.com/cgmcoding/p/13212479.html
Copyright © 2011-2022 走看看