How to remove white spaces from a string in Python?

前端 未结 6 1833
小蘑菇
小蘑菇 2020-12-10 00:27

I need to remove spaces from a string in python. For example.

str1 = \"TN 81 NZ 0025\"

str1sp = nospace(srt1)

print(str1sp)

>>>TN81NZ0025
         


        
6条回答
  •  被撕碎了的回忆
    2020-12-10 00:54

    You can replace multiple spaces into a desired pattern by using following ways. Here your pattern is blank string.

    import re
    pattern = ""
    re.sub(r"\s+", pattern, your_string)
    

    or

    import re
    pattern = ""
    re.sub(r" +", "", your_string)
    

提交回复
热议问题