Home  >  Article  >  Backend Development  >  How to solve the problems of character conversion, string splitting and string merging in Python strings

How to solve the problems of character conversion, string splitting and string merging in Python strings

WBOY
WBOYforward
2023-05-23 10:28:051092browse

    1. Character conversion of strings

    1.1. Concept of character conversion

    The string replacement mentioned earlier is Replace a substring in the string with a new substring. If we want to convert certain characters in the string, that is, replace a single character in the string, we can call the methods maketrans and translate to achieve this. .

    First call the maketrans method to create a conversion table, declare in the conversion table what characters to convert those characters into, and then pass the created conversion table as a parameter to the translate method to implement character conversion.

    The syntax of the maketrans method to create a conversion table:

    str.maketrans('转换的字符', '转换后的字符','删除的字符')

    The maketrans method can pass in three parameters:

    • The first parameter is used to specify the Converted characters, if multiple characters are specified, then multiple converted characters also need to be specified, and the numbers must be the same. The first character to be converted will be converted to the first character in the second parameter, case The meeting will explain.

    • The second parameter is used to specify the converted characters.

    • The third parameter is used to specify which characters to delete.

    After defining a conversion table, you can use print to print the contents of the conversion table. At this time, a dictionary is returned, and each key:value in the dictionary corresponds to a character. ordinal value.

    The translate method passes in the conversion table as a parameter, and then calls it with the defined string to finally achieve character conversion.

    1.2. Application cases of character conversion

    1) Creation of conversion table

    When defining the conversion table, if multiple characters to be converted are specified characters, then the number of characters after conversion must be the same as the number of characters being converted. At this time, the first parameter and the second parameter will be cross-converted. The code is as follows.

    contable = str.maketrans('agx', '789')
    print(contable)
    
    '''
    代码解释:
        str.maketrans('agx', '789')定义了一个转换表,其中字符a转换成字符7,字符g转换成字符8,字符x转换成字符9
        当使用print函数打印定义的转换表时,返回的是一个字典,key是被转换的字符,value是转换后的字符,并且返回的是字符的ordinal value值
        返回结果:{97: 55, 103: 56, 120: 57}
            {97是字符a: 55是字符7, 103是字符g: 56是字符8, 120是字符x: 57是字符9}
    '''

    You can use the print function to print the conversion table. The return result is a dictionary. The value in the dictionary is the ordinal value of the corresponding character. In the dictionary, you can clearly see what characters will be converted into what characters. As shown in the figure below, 97 is the character a, 55 is the character 7, and the characters corresponding to 97 will be converted into the characters corresponding to 55.

    How to solve the problems of character conversion, string splitting and string merging in Python strings

    In addition to directly declaring the converted characters and the original values ​​​​of the converted characters in the form of strings, you can also use the dictionary method to declare conversion and converted characters, because The result returned by the conversion table is a dictionary, but the most common method is to declare the converted characters directly in the conversion table.

    #使用字典的形式,在字典中声明转换的字符、被转换字符的原始值
    contable = str.maketrans({'a': '7', 'g': '8', 'x': '9'})
    print(contable)
    '''
    	此时使用print打印转换表时,就会发现转换后的字符会以原始值的形式记录在字典中,被转换的字符还是以ordinal value值表示
    '''
    #输出结果:{97: '7', 103: '8', 120: '9'}
    
    #还是使用字典的行驶,在字典中声明被转换字符、转换字符的ordinal value值
    contable = str.maketrans({97: 55, 103: 56, 120: 57})
    print(contable)
    '''
    	这种方法需要实现使用ord函数获取被转换字符、转换字符的ordinal value值,然后在字典中直接声明字符的ordinal value值
    '''
    #输出结果:{97: 55, 103: 56, 120: 57}

    2) Character conversion

    #首先定义一个转换表,这个转换表可以被任意字符串调用使用
    contable = str.maketrans('agx', '789')
    
    #定义一个字符串
    mystr = 'jiangxluplader'
    
    #使用translate方法将转换表以参数的形式传入,然后由字符串调用这个方法实现字符的转换
    print(mystr.translate(contable))
    
    #输出结果:ji7n89lupl7der

    It was found that all a characters in the string were converted to 7, and all g characters were converted to 8 , all x characters are converted to 9.

    How to solve the problems of character conversion, string splitting and string merging in Python strings

    3) When the maketrans method defines the conversion table, you can also specify the characters to be deleted, such as deleting the characters u, p

    contable = str.maketrans('agx', '789', 'up')
    mystr = 'jiangxluplader'
    print(mystr.translate(contable))
    
    '''
    	maketrans方法创建转换表时,第三个参数是指定删除的字符
    '''
    
    #输出结果:ji7n89ll7der

    How to solve the problems of character conversion, string splitting and string merging in Python strings

    4) In the maketrans method definition conversion table, you can also not convert characters and only delete certain characters, such as only deleting characters u and p

    contable = str.maketrans('', '', 'up')
    mystr = 'jiangxluplader'
    print(mystr.translate(contable))
    
    #输出结果:jiangxllader

    How to solve the problems of character conversion, string splitting and string merging in Python strings

    When the third parameter is specified, the specified character will be deleted. In fact, the specified character will be set to a None object.

    How to solve the problems of character conversion, string splitting and string merging in Python strings

    2. String splitting

    2.1. The concept of string splitting

    The so-called string splitting refers to Yes, the string is divided into N pieces according to the specified delimiter, such as the string "aaa bbb". The default delimiter is a space. According to the delimiter, the string can be divided into "aaa", "bbb", This is the splitting of the string. There are two methods for splitting strings:

    1) Call the method split or rsplit to split the string

    • The split method starts from characters Start splitting the string from the left side of the string, and start splitting from the front to the back.

    • rsplit method is to split the string starting from the right side of the string and starting from the back to the front.

    The split and rsplit methods will split all the places where the delimiter appears in the string according to the delimiter.

    The default delimiter is a space string. If the maximum number of splits is not specified by these two methods, there will be no difference in the output content, and the return value of both methods is a list.

    当字符串中没有指定的分隔符时,这两个方法都会返回字符串本身,但是也会将字符串本身放在列表中。

    2)调用方法partition或者rpartition劈分字符串

    partition和rpartition这两个方法在调用的时候必须指定分隔符,然后对字符串进行劈分,这两个方法对字符串劈分后会返回一个元组。

    • partition方法是左劈分,从左往右找到分隔符第一次出现的位置,然后开始劈分字符串,始终劈分成三个元素,第一个元素是分隔符左侧的部分,第二个元素是分隔符,第三个元素是分隔符后面的部分。

    • rpartition方法是右劈分,从右往左找到分隔符第一次出现的位置,或者从左往右找到分隔符最后一次出现的位置,然后开始劈分字符串,始终劈分成三个元素,第一个元素是分隔符左侧的部分,第二个元素是分隔符,第三个元素是分隔符后面的部分。

    partition方法和rpartition只会将字符串中第一次出现分隔符的位置,进行劈分,一分为三,这就是与split方法的最大区别

    partition方法和rpartition两个方法只是开始劈分的位置处不同,然后将字符串劈分为三个元素:

    • 分隔符前面的部分

    • 分隔符

    • 分隔符后面的部分

    如果字符串中不存在指定的分隔符,两个方法都会返回字符串本身,partition方法劈分的三个元素顺序依次为:字符串本身、空字符串、空字符串,rpartition方法劈分的三个元素依次为:空字符串、空字符串、字符串本身。

    2.2.调用方法split或者rsplit劈分字符串

    1)基本的劈分案例

    #原始字符串
    mystr = "DevOps Jenkins Kubernetes"
    
    #左劈分
    print(mystr.split())
    
    #右劈分
    print(mystr.rsplit())
    
    '''
    	左劈分是从DevOps第一个空格字符串处开始劈分,右劈分是从Kubernetes最后一个空格字符串处开始劈分
    	默认的劈分字符串是空格字符串,只要遇到空格字符串,就将空格字符串左侧的字符串劈分出来。
    '''

    可以看到split和rsplit两个方法对字符串劈分后,并没有明显的区别。

    How to solve the problems of character conversion, string splitting and string merging in Python strings

    2)通过参数sep指定分隔符劈分字符串

    在split和rsplit方法中都可以使用sep参数指定分隔符,从而根据指定的分隔符去劈分字符串。

    mystr = "DevOps|Jenkins|Kubernetes"
    print(mystr.split(sep='|'))
    print(mystr.rsplit(sep='|'))
    
    '''
    	split(sep='|')、rsplit(sep='|')指定分隔符为“|”
    '''

    可以看到split和rsplit两个方法对字符串劈分后,并没有明显的区别。

    How to solve the problems of character conversion, string splitting and string merging in Python strings

    当指定的分隔符字符串不存在时,则会返回字符串本身,但是也会将字符串本身放在列表中。

    mystr = "DevOps Jenkins Kubernetes"
    print(mystr.split(sep='|'))
    print(mystr.rsplit(sep='|'))

    How to solve the problems of character conversion, string splitting and string merging in Python strings

    3)通过参数maxsplit指定劈分字符串的最大劈分次数

    在split和rsplit方法中都可以使用maxsplit参数指定劈分字符串的最大劈分次数,什么意思呢?原本一个字符串可以被劈分6次,但是我们只需要劈分2次就行,就可以使用maxsplit参数指定指定劈分字符串的最大劈分次数。

    当指定了最大的劈分次数时,当超过最大的劈分次数后,剩余的子串会单独作为一部分输出,此时split和rsplit两个劈分方法就会有明显的区别。

    #定义原始字符串
    mystr = "DevOps|Jenkins|Kubernetes|Python|Gitlab"
    
    '''
    	使用split方法是从左往右开始劈分,从第一个子串位置处开始劈分,分隔符为"|",劈分次数为2,本身这个字符串可以被劈分5次,我们只劈分2次,那么DevOps、Jenkins会被劈分出来作为列表的单独元素,而剩余的子串Kubernetes|Python|Gitlab会被作为一部分放在列表的一个元素内
    '''
    print(mystr.split(sep='|', maxsplit=2))
    #输出结果:['DevOps', 'Jenkins', 'Kubernetes|Python|Gitlab']
    
    '''
    	使用rsplit方法是从右往左开始劈分,从最后一个子串处开始劈分,分隔符为"|",劈分次数为2,本身这个字符串可以被劈分5次,我们只劈分2次,那么Gitlab、Python这两个字符串会被劈分出来作为列表的单独元素,而剩余的字符DevOps|Jenkins|Kubernetes会被作为一部分,放在列表的一个元素内
    '''
    print(mystr.rsplit(sep='|', maxsplit=2))
    #输出结果:['DevOps|Jenkins|Kubernetes', 'Python', 'Gitlab']

    当在劈分方法内指定了最大的劈分次数的参数时,split左劈分和rsplit右劈分就会有明显的区别,split方法是从前往后根据分隔符进行劈分,rsplit方法是从后往前根据分隔符进行劈分。

    How to solve the problems of character conversion, string splitting and string merging in Python strings

    2.3.调用方法partition或者rpartition劈分字符串

    partition方法是左劈分,从左往右找到分隔符第一次出现的位置,然后开始劈分字符串,始终劈分成三个元素,第一个元素是分隔符左侧的部分,第二个元素是分隔符,第三个元素是分隔符后面的部分。

    rpartition方法是右劈分,从右往左找到分隔符第一次出现的位置,或者从左往右找到分隔符最后一次出现的位置,然后开始劈分字符串,始终劈分成三个元素,第一个元素是分隔符左侧的部分,第二个元素是分隔符,第三个元素是分隔符后面的部分。

    这两个方法,只会根据一个分隔符进行劈分字符串,且始终劈分成三个元素,partition方法的分隔符取决于在字符串中第一次出现的分隔符,rpartition方法的分隔符取决于字符串中最后一次出现的分隔符。

    1)调用方法partition劈分字符串

    mystr = "DevOps|Jenkins|Kubernetes|Python|Gitlab"
    print(mystr.partition('|'))
    
    '''
    	partition从左往右找到第一次出现分隔字符串的位置,从这里开始劈分字符串,分隔符左侧的部分(DevOps)为第一个元素,分隔符(|)为第二个元素,分隔符右侧的部分(Jenkins|Kubernetes|Python|Gitlab)为第三个元素。
    '''
    
    #输出结果:('DevOps', '|', 'Jenkins|Kubernetes|Python|Gitlab')

    How to solve the problems of character conversion, string splitting and string merging in Python strings

    2)调用方法rpartition劈分字符串

    mystr = "DevOps|Jenkins|Kubernetes|Python|Gitlab"
    print(mystr.rpartition('|'))
    
    '''
    	rpartition从左往右找到最后一次出现分隔字符串的位置,分隔符左侧的部分(DevOps)为第一个元素,分隔符(|)为第二个元素,分隔符右侧的部分(Jenkins|Kubernetes|Python|Gitlab)为第三个元素。
    '''
    
    #输出结果:('DevOps|Jenkins|Kubernetes|Python', '|', 'Gitlab')

    确实和理论说明的一样,从左往右找到最后一次分隔符出现的位置,分隔符左侧的部分为元组中的第一个元素,分隔符为第二个元素,分隔符右侧的部分为元组中的第三个元素。

    How to solve the problems of character conversion, string splitting and string merging in Python strings

    3)当字符串中只有一个分隔符字符串且分隔符后没有任何字符时

    当字符串中只有一个分隔符字符串时,且分隔符后面没有任何字符串,此时无论是partition方法还是rpartition方法,劈分的字符串第三个元素一定为空。

    mystr = "DevOps|"
    print(mystr.partition('|'))
    print(mystr.rpartition('|'))

    第三个元素取的都是分隔符后面的部分,分隔符后面的部分什么也没有,因此就会输出空字符串。

    How to solve the problems of character conversion, string splitting and string merging in Python strings

    4)当字符串中不存在指定的分隔符字符串(partition方法)

    如果字符串中不存在指定的分隔符,partition方法劈分的三个元素顺序依次为:字符串本身、空字符串、空字符串。

    mystr = "DevOps|Jenkins|Kubernetes|Python|Gitlab"
    print(mystr.partition('*'))
    
    #输出结果:('DevOps|Jenkins|Kubernetes|Python|Gitlab', '', '')

    第一个元素是字符串的本身,第二个元素是空字符串,第三个元素也是空字符串。

    How to solve the problems of character conversion, string splitting and string merging in Python strings

    4)当字符串中不存在指定的分隔符字符串(rpartition方法)

    如果字符串中不存在指定的分隔符,rpartition方法劈分的三个元素顺序依次为:空字符串、空字符串、字符串本身。

    mystr = "DevOps|Jenkins|Kubernetes|Python|Gitlab"
    print(mystr.rpartition('*'))

    第一个元素是空字符串,第二个元素是空字符串,第三个元素是字符串的本身。

    How to solve the problems of character conversion, string splitting and string merging in Python strings

    3.字符串的合并

    可以调用方法join将多个字符串合并成一个字符串。语法格式如下:

    '字符串的间隔符'.join(列表|数组)
    
    字符串的间隔符可以指定,也可以不指定,指定后,每个元素字符串在合并时都会在后面添加一个分隔符。

    1)使用join方法将列表中多个字符串进行合并

    使用join方法可以将列表中的多个元素,元素也都是字符串,将这些字符串进行合并。

    print(''.join(["Python", "Go", "K8S"]))
    
    #输出结果:PythonGoK8S

    使用join方法将列表中的每个元素合并时添加一个分隔符“|”

    print('|'.join(["Python", "Go", "K8S"]))
    
    #输出结果:Python|Go|K8S

    2)使用join方法将元组中的多个字符串进行合并

    join方法也可以将元组中的元素合并。

    print('|'.join(("Python", "Go", "K8S")))
    
    #输出结果:Python|Go|K8S

    3)可以把字符串看成是字符的列表,然后使用join方法将这些字符合并成字符串

    可以把字符串看成是字符的列表,然后使用join将这些字符加上分隔符合并成新的字符串。

    print('|'.join("Python"))
    
    #输出结果:P|y|t|h|o|n

    How to solve the problems of character conversion, string splitting and string merging in Python strings

    The above is the detailed content of How to solve the problems of character conversion, string splitting and string merging in Python strings. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete