Python. Functions for breaking strings into parts and forming new strings using tuples and lists




Functions for breaking strings into parts and forming new strings using tuples and lists


Contents


Search other websites:

1. Function str.join(). Collect a string from the list

The str.join() function returns a string, which is the union of strings in an iterated object. If the iterated object has non-string values, the function will throw a TypeError exception.

According to the Python documentation, using a function has the following form:

s1 = s2.join(iterable)

where

  • s1 – a string that is created as a result of combining the strings of an iterable object;
  • s2 – delimiter string.

Example.

# Function str.join() - collect string from a list

# The list of strings
lst = ['abc', 'def', 'ghi']

s1 = ''
s2 = s1.join(lst) # s2 = 'abcdefghi'

# The list of characters
SYMBOLS = list("Hello") # ITEMS = ['H', 'e', 'l', 'l', 'o']

s1 = ""
s3 = s1.join(SYMBOLS) # s3 = 'Hello'
s4 = str.join(s1, SYMBOLS) # s4 = 'Hello'

# Separator - string ' => '
s1 = ' => '
s5 = s1.join(['012', '345', '678']) # s5 = '012 => 345 => 678'

 

2. Function str.partition(). Break a string into a tuple starting at the beginning of a string

The str.partition() function splits a string into a tuple of 3 elements based on the specified separator character. Unlike the str.rpartition() function, in this function the string is reviewed from beginning to end.

The general form of using the function is as follows:

t = str.partition(sep)

where

  • str – string, which is being considered for splitting;
  • t – the resulting tuple consisting of 3 strings. The first string is the part of the string before the sep delimiter. The second string is the sep separator itself. The third string is the part of the string after the sep delimiter;
  • sep – character-separator.

If no separator is found, then the function returns a tuple of 3 strings, in which the string itself follows first, then two empty strings.

Example.

# Function str.partition() - splits a string based on a separator character

# 1. Case when separator is found
s1 = 'abc,def,ghi'
s2 = s1.partition(',') # s2 = ('abc', ',', 'def,ghi')

s2 = 'abcdef'.partition('d') # s2 = ('abc', 'd', 'ef')

s1 = '012+345'
s2 = str.partition(s1, '+') # s2 = ('012', '+', '345')

# 2. The case where the separator is not found
s1 = '12345'
s2 = s1.partition('=') # s2 = ('12345', '', '')

# 3. The case where there are several separators - the string viewed from the begin
s1 = '012+345+65+89'
s2 = s1.partition('+') # s2 = ('012', '+', '345+65+89')

 

3. Function str.rpartition(). Split a string into a tuple starting at the end of a string

The str.rpartition() function splits the string into a tuple of 3 items based on the specified separator character. Unlike the str.partition() function, in this function the string is reviewed from the end.

The general form of using the function is as follows:

t = str.rpartition(sep)

where

  • str – a string that is considered for splittin;
  • t – resulting tuple consisting of 3 strings. The first string is the part of the string before the sep delimiter. The second string is the sep separator itself. The third string is the part of the string after the sep delimiter;
  • sep – character-separator.

If the separator is not found, then the function returns a tuple of 3 strings, in which the first two strings are empty, the third string is the string itself.

Example.

# Function str.rpartition() - splits a string based on a separator character

# 1. The case where the separator is found
s1 = 'abc,def,ghi'
s2 = s1.rpartition(',') # s2 = ('abc,def', ',', 'ghi')

s2 = 'abcdef'.rpartition('d') # s2 = ('abc', 'd', 'ef')

s1 = '012+345'
s2 = str.rpartition(s1, '+') # s2 = ('012', '+', '345')

# 2. The case where the separator is not found
s1 = '12345'
s2 = s1.rpartition('=') # s2 = ('', '', '12345')

# 3. The case when several separators - the string is reviewed from the end
s1 = '012+345+65+89'
s2 = s1.rpartition('+') # s2 = ('012+345+65', '+', '89')


 

4. Function str.rsplit(). Generate a word list based on a separator character

The str.rsplit() function generates a list of words formed from a string based on a delimiter character. The function works the same as the str.split() function only with the difference that the formed words are divided from right to left.

The general form of using the function is as follows:

setStr = str.rsplit(sep = None, maxsplit = -1)

here

  • setStr – the resulting list of strings (words) that are formed from the string str;
  • str – source string, which is split into many words;
  • sep – word separator, which may consist of one or more characters. If the sep separator is specified, then the separators themselves are not grouped together and are considered empty string separators. If you try to split the empty string with the specified separator, then a list is returned containing the empty string [”];
  • maxsplit – the maximum number of splits the string str can be split into. For example, if maxsplit = 2, then the list will contain no more than 3 elements (maxsplit + 1 elements). If the maxsplit value is not specified or equal to -1, then the number of partitions is unlimited, in this case all possible string breaks are performed.

Example.

# Function str.rsplit() - splitting a string by a character-separator

# 1. Function call without sep and maxsplit parameters
s1 = 'a,b,c,d'
s2 = s1.rsplit() # s2 = ['a,b,c,d'] - one word, space is taken into account

s1 = 'a b c def'
s2 = s1.rsplit() # s2 = ['a', 'b', 'c', 'def'] - 4 words, space taken into account

s1 = 'abc hello,+ fgh 0123 '
s2 = str.rsplit(s1) # s2 = ['abc', 'hello,+', 'fgh', '0123']

# 2. Function call with sep parameter
s1 = 'a,b,c,d'
s2 = s1.rsplit(',') # s2 = ['a', 'b', 'c', 'd'], character-separator ','

# character-separator '!='
s2 = 'a!=b!=c!=d+5'.rsplit('!=') # s2 = ['a', 'b', 'c', 'd+5']

# 3. Calling a function with parameters sep, maxsplit
# with such a call, the result of the function differs from the result of split()
s1 = 'a,b,c,d,e'
s2 = s1.split(',', 2) # s2=['a', 'b', 'c,d,e'] - from the beginning to the end
s3 = s1.rsplit(',', 2) # s3=['a,b,c', 'd', 'e'] - from end to beginning

s1 = 'a+b+c+d'
s2 = s1.split('+', 2) # s2 = ['a', 'b', 'c+d'] - from the beginning to the end
s3 = s1.rsplit('+', 2) # s3 = ['a+b', 'c', 'd'] - from end to beginning

 

5. Function str.split(). Separate a string by a delimiter character

The str.split() function receives a string and returns a list of words formed from this string that are separated (highlighted) by the specified separator character.

According to the Python documentation, the general form of using a function is as follows:

setStr = str.split(sep = None, maxsplit = -1)

here

  • setStr – the resulting list of strings (words) formed from the string str;
  • str – source string, which is separated into many words;
  • sep – word separator, which may consist of one or more characters. If the sep delimiter is specified, then the delimiters themselves are not grouped together and are considered empty string separators. If you try to split an empty string with the specified delimiter, then a list is returned containing the empty string [”];
  • maxsplit – the maximum number of breakdowns the string str can be split into. For example, if maxsplit = 2, then the list will contain no more than 3 items (maxsplit + 1 items). If maxsplit is not specified or equal to -1, then the number of breakdowns is unlimited. In this case, all possible string breaks are performed.

Example.

# Function str.split() - splitting a string by a delimiter character

# 1. Function call without sep and maxsplit parameters
s1 = 'a,b,c,d'
s2 = s1.split() # s2 = ['a,b,c,d'] - one word, space is taken into account

s1 = 'a b c def'
s2 = s1.split() # s2 = ['a', 'b', 'c', 'def'] - 4 words, space taken into account

s1 = 'abc hello,+ fgh 0123 '
s2 = str.split(s1) # s2 = s2 = ['abc', 'hello,+', 'fgh', '0123']

# 2. Function call with sep parameter
s1 = 'a,b,c,d'
s2 = s1.split(',') # s2 = ['a', 'b', 'c', 'd'], character-separator ','

# character-separator '=='
s2 = 'a==b==c==d+5'.split('==') # s2 = ['a', 'b', 'c', 'd+5']

# 3. Function call with sep, maxsplit parameters
s1 = 'a,b,c,d,e'
s2 = s1.split(',', 2) # s2=['a', 'b', 'c,d,e'] - maximum 2 breakdowns (3 words)

s1 = 'a+b+++c+def++ghi'
s2 = s1.split('+',3) # s2=['a', 'b', '', '+c+def++ghi'] - character '+' replaced by ''

 

6. Function str.splitlines(). Get a list of strings based on a string that contains strings separators

The str.splitlines() function forms a list of strings based on a given string in such a way that characters that serve as string separators are taken into account.

According to the Python documentation, the general form of using a function is as follows:

strList = str.splitlines([keepends])

where

  • strList – the resulting list of strings divided by string boundaries;
  • str – a string containing characters that separate strings. Such characters include, for example, the ‘\n’ character of a new line (see table below);
  • keepends – optional parameter that defines the inclusion of separator characters in the resulting string strList. If keepends = True, then delimiter characters are included in the result string.

The characters used to split the string are shown in the table below.

Python. Characters used to split the string

Example.

# Function str.splitlines()

# 1. Function call without parameters
s1 = 'ab\ncd\ref'
s2 = s1.splitlines() # s2 = ['ab', 'cd', 'ef']

s1 = '012\x1d345\x1e6789'
s2 = str.splitlines(s1) # s2 = ['012', '345', '6789']

s2 = 'abc\u2028defg'.splitlines() # s2 = ['abc', 'defg']

# 2. Function call with parameter - separator characters included
s1 = 'ab\ncd\r\nef'
s2 = s1.splitlines(True) # s2 = ['ab\n', 'cd\r\n', 'ef']

s2 = 'abc\u2029def'.splitlines(True) # s2 = ['abc\u2029', 'def']

s2 = '\n\n\r\n'.splitlines(False) # s2 = ['', '', '']
s2 = '\n\n\r\n'.splitlines(True) # s2 = ['\n', '\n', '\r\n']

 


Related topics