How to strip all whitespace from string

前端 未结 11 1909
暖寄归人
暖寄归人 2020-11-28 18:25

How do I strip all the spaces in a python string? For example, I want a string like strip my spaces to be turned into stripmyspaces, but I cannot s

11条回答
  •  佛祖请我去吃肉
    2020-11-28 18:50

    As mentioned by Roger Pate following code worked for me:

    s = " \t foo \n bar "
    "".join(s.split())
    'foobar'
    

    I am using Jupyter Notebook to run following code:

    i=0
    ProductList=[]
    while i < len(new_list): 
       temp=''                            # new_list[i]=temp=' Plain   Utthapam  '
       #temp=new_list[i].strip()          #if we want o/p as: 'Plain Utthapam'
       temp="".join(new_list[i].split())  #o/p: 'PlainUtthapam' 
       temp=temp.upper()                  #o/p:'PLAINUTTHAPAM' 
       ProductList.append(temp)
       i=i+2
    

提交回复
热议问题