There are two solutions that I have come up with , One is  .replace(' old ',' new ')   The first parameter is what needs to be replaced, such as spaces , The second is to replace it with , You can replace all the spaces in the string .

The second way is like this

 str_1_data = ' a b c '
str_2_list = str_1_data.split()
str_1 = ''
for i in range(len(str_2_list)): # You can use it directly here str_1.join(str2_list)
str_1 += str_2_list[i]
print(str_1)

First use .split(str,num)

(str -- Separator , The default is all empty characters , Including Spaces 、 Line break (\n)、 tabs (\t) etc. , Can be null .

num -- Number of divisions , Can be null .)

Cut the source string into a list , The parameter in it is which character is used as the incision , Default is space , Then connect each string in turn , The running result is : >>>abc

in addition ,rel There's another one in it re,split('re',str) It can match strings with regular expressions to split :

a = re.split(r'\W','hello,world') #\w All alphanumeric underscores ,\W All non alphanumeric underscores 
print(a) >>> ['hello', 'world']

If the regular expression has parentheses , It will also match the contents of regular expressions , Such as :

a = re.split(r'(\W)','hello,world')
print(a) >>> ['hello', ',', 'world']

Python_ How to remove the space in the string

  1. js Remove the space in the middle of a string

    function trims(str){ return str.replace(/[ ]/g,""); // Remove the space in the middle of a string }

  2. shell Realization trim function - Remove the space around the string ( Include tab,space key )

    shell Realization trim Function effect to remove the space on both sides of the string , The following three commands are equivalent , Can achieve sed 's/^\s*//' totrim.txt |sed 's/\s*$//'>trimed.txtsed ' ...

  3. 【C Language 】 String replace space : Implement a function , Replace the space in the string with “%20”

    // String replace space : Implement a function , Replace the space in the string with "%20" #include <stdio.h> #include <assert.h> void ...

  4. 【Python practice -4】 Slicing removes spaces at the beginning and end of a string

    # Using slicing , Achieve one trim() function , Remove the space at the beginning and end of the string , Be careful not to call str Of strip() Method def trim(s): while s[0:1]==' ': s=s[1:] while s[ ...

  5. Linux shell Remove all spaces in the string

    Linux shell Remove all spaces in the string echo $VAR | sed 's/ //g'

  6. js Remove the spaces on both sides of the string from the string

    When you submit a form, you need to remove the spaces on both sides of the string , The code is as follows : /* Remove the space around the string */ String.prototype.trim = function() { return this.replace(/ ...

  7. js Remove the spaces on both sides of the string

    js Remove the spaces on both sides of the string function trim(str){ // Delete the left and right spaces           return str.replace(/(^\s*)|(\s*$)/g, "&q ...

  8. java Go to html label , Remove the space in the string , enter , A newline , tabs

    public static String getonerow(String allLine,String myfind)     {                           Pattern ...

  9. Using slicing , Achieve one trim() function , Remove the space at the beginning and end of the string , Be careful not to call str Of strip() Method

    1. Using slicing , Achieve one trim() function , Remove the space at the beginning and end of the string , Be careful not to call str Of strip() Method First, determine whether the length of the string is 0, If it is , Return string directly second , Loop to determine if there is a space at the beginning of a string , Such as ...

Random recommendation

  1. Something about pip in Python

    Study python The crawler frame scrapy When , be used pip Manage tools to install scrapy. The most intuitive feeling ,pip It's kind of like Ubuntu Under the apt-get Tools , A few lines of code , Automatic download . Installation related things . I looked up some background on the Internet ...

  2. Git Push Avoid user name and password methods

    Reference here : http://www.cnblogs.com/ballwql/p/3462104.html The first method works

  3. No.010:Regular Expression Matching

    problem : Implement regular expression matching with support for '.' and '*'.'.' Matches any single charac ...

  4. .net When using configuration files in, you need to pay attention to references dll file

    Need to use sqlhelper And configuration file using System.Configuration; Still can't use ConfigurationManager. Checked several times msdn failed , Baidu directly found that it still needs to quote ...

  5. java Foundation finishing 1

    1. Anonymous object :new persion().tell(); In this way , It just opens up stack memory , There is no stack reference relationship 2. The name of the constructor must match the name of the class , There cannot be any declaration of return value type at the declaration of a constructor , Cannot be used in construction methods r ...

  6. Nginx Unable to read under session Lead to thinkphp Verification code error

    open php The configuration file php.ini Use the search command whereis php.ini Generally in :/etc/php.ini Under the table of contents Use vim Command to open find : session.save_path find php Protect ...

  7. jQuery Auto complete text box

    jQuery Automatically complete plug-in open source software http://www.oschina.net/project/tag/329/jquery-autocomplete jQuery TextExt http://te ...

  8. asp.net The whole process from client request to response

    Source :https://www.cnblogs.com/Joans/archive/2012/02/08/2342887.html doubt ? Make a request from the client , How does the request arrive at the server and IIS Connect ?IIS ...

  9. Linux DNS Query analysis ( The fourth part ) | Linux China

    Copyright notice : This article is an original blog article , It can't be reproduced without the permission of the blogger . https://blog.csdn.net/F8qG7f9YD02Pe/article/details/82879414 In the fourth part , I'll introduce Rong ...

  10. JavaSE Basic knowledge of (3)— Process control structure

    One . Sequential structure 1. explain The program runs from top to bottom , There's no jump or choice in the middle 2. characteristic Variables must follow “ The forward references ” ( Local variables must be declared first . assignment , And then use !) Two . Branching structure ( Conditions ) 1. explain The program takes two or more paths ...