Open In App

Python | K Character Split String

Improve
Improve
Like Article
Like
Save
Share
Report

The problems and at the same time applications of list splitting is quite common while working with python strings. Some characters are usually tend to ignore in the use cases. But sometimes, we might not need to omit those characters but include them in our programming output. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using split() + list comprehension This kind of operation can be performed using the split function and list comprehension. The main difference in not omitting the character is that we specifically add the characters that we might have omitted in the process, after each element. 

Python3




# Python3 code to demonstrate
# K Character Split String
# using list comprehension + split()
 
# initializing string
test_string = "GfG_is_Best"
 
# printing original string
print("The original string : " + str(test_string))
 
# initializing K
K = '_'
 
# using list comprehension + split()
# K Character Split String
res = [i for j in test_string.split(K) for i in (j, K)][:-1]
 
# print result
print("The list without omitting Character : " + str(res))


Output : 

The original string : GfG_is_Best
The list without omitting Character : ['GfG', '_', 'is', '_', 'Best']

The time complexity of the code is O(nm), where n is the number of strings in the list and m is the average length of the strings. The reason for this is that for each string in the list, the code needs to iterate through its characters and check if it is an upper case character. This takes O(m) time for each string.
The space complexity is O(nm) because of the use of multiple lists to store intermediate results.

  Method #2 : Using zip() + chain() + cycle() This particular task can also be performed using the combination of above 3 functions. The zip function can be used to bind the logic, chain and cycle function perform the task of inserting the character at appropriate position. 

Python3




# Python3 code to demonstrate
# K Character Split String
# using zip() + chain() + cycle()
from itertools import chain, cycle
 
# initializing string
test_string = "GfG_is_Best"
 
# printing original string
print("The original string : " + str(test_string))
 
# initializing K
K = "_"
 
# using zip() + chain() + cycle()
# K Character Split String
res = list(chain(*zip(test_string.split(K), cycle(K))))[:-1]
 
# print result
print("The list without omitting character : " + str(res))


Output : 

The original string : GfG_is_Best
The list without omitting Character : ['GfG', '_', 'is', '_', 'Best']

Time complexity: O(m*n), where n is the number of strings in the list and m is the average length of the strings.
Auxiliary space: O(m*n) as well, because of the use of multiple lists to store intermediate results.

Method #3 : Using split() method

Python3




# Python3 code to demonstrate
# K Character Split String
 
# initializing string
test_string = "GfG_is_Best"
 
# printing original string
print("The original string : " + str(test_string))
 
# initializing K
K = '_'
ns=""
for i in test_string:
    if(i==K):
        ns+="*"+K+"*"
    else:
        ns+=i
res=ns.split("*")
 
# print result
print("The list without omitting Character : " + str(res))


Output : 

The original string : GfG_is_Best
The list without omitting Character : [‘GfG’, ‘_’, ‘is’, ‘_’, ‘Best’]

Time Complexity: O(m*n), where n is the number of strings in the list and m is the average length of the strings.
Auxiliary Space: O(m*n), where n is the number of strings in the list and m is the average length of the strings.

Method#4:Using for loop

  1. Initialize an empty list res.
  2. Initialize an empty string curr.
  3. Loop through each character c in the input string test_string:
  4. If the current character c equals the split character K, then:
  5. Append the current string curr to the res list.
  6. Append the split character K to the res list.
  7. Reset the curr string to an empty string.
  8. Otherwise, add the current character c to the curr string.
  9. Append the remaining curr string to the res list.
  10. Return the res list.

Python3




test_string = "GfG_is_Best"
K = '_'
res = []
curr = ''
for c in test_string:
    if c == K:
        res.append(curr)
        res.append(K)
        curr = ''
    else:
        curr += c
res.append(curr)
print(res)
#This code is contributed by Vinay Pinjala.


Output

['GfG', '_', 'is', '_', 'Best']

Time Complexity:

Step 3 takes O(n) time, where n is the length of the input string test_string.
Steps 3.1 and 3.2 take constant time.
Therefore, the overall time complexity is O(n).
Auxiliary Space:

The space used by the list res is proportional to the number of split elements, which is at most n.
Therefore, the overall space complexity is O(n).



Last Updated : 13 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads