Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the common methods of removing spaces from strings in Python

Shulou Source: shulou.com Published: 2022-06-02 17:31:49 10月31日 Update

This article will explain in detail the methods of removing spaces from common strings in Python. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

1:strip () method

Remove the space at the beginning or end of the string

> a = "a b c" > a.strip () 'a b c'2:lstrip () method

Remove the space at the beginning of the string

> a = "a b c" > a.lstrip ()'a b c '3:rstrip () method

Remove the space at the end of the string

A: there are no answers to problems encountered in study. The editor has created a Python learning exchange group: 531509025 look for like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! > a = "a b c" > a.rstrip () 'a b c'4:replace () method

All spaces can be removed.

Replace is mainly used to replace replace (old, new, count) of strings.

> a = "abc" > > a.replace (",") 'abc'5: join () method + split () method

All spaces can be removed.

Join passes in a list of strings for character string synthesis. Split is used for string segmentation and can be divided according to rules.

> a = "a b c" > b = a.split () # string is divided into lists by spaces > b ['asides,' b' 'c'] > c = ".join (b) # compose list contents with an empty string to generate a new string > c 'abc' # shortcut usage > a =" abc ">" .join (a.split ())' abc' about what are the common methods for removing spaces in Python? Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Tags: Characters strings methods spaces articles learning common good content beginning more practical like-minded help each other help each other learn and communicate study tutorials boys buddies tutorials Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei MariaDB Microsoft macOS Shulou Tech Info