Open In App

Python – Separate first word from String

Last Updated : 18 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python strings, we can have a problem in which we need to separate the first word from the entire string. This can have possible applications in many domains such as day-day and web-development. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using loop This is the brute force way in which this task can be performed. In this, we iterate the string and extract the word till the 1st word and then append other strings as other elements of list. 

Python3




# Python3 code to demonstrate working of
# Separate first word from String
# Using loop
 
# initializing string
test_str = "gfg is best"
 
# printing original string
print("The original string is : " + test_str)
 
# Separate first word from String
# Using loop
res = []
temp = ''
flag = 1
for ele in test_str:
    if ele == ' ' and flag:
        res.append(temp)
        temp = ''
        flag = 0
    else :
        temp += ele
res.append(temp)
                     
# printing result
print("Initial word separated list : " + str(res))


Output : 

The original string is : gfg is best
Initial word separated list : ['gfg', 'is best']

Time Complexity: O(N), where N is the size of the given string.
Auxiliary Space: O(N)

  Method #2: Using split() This is recommended and a one-liner that can be used to perform this task. In this, we just split the string by space and then construct the list of separated words. 

Python3




# Python3 code to demonstrate working of
# Separate first word from String
# Using split()
 
# initializing string
test_str = "gfg is best"
 
# printing original string
print("The original string is : " + test_str)
 
# Separate first word from String
# Using split()
res = test_str.split(' ', 1)
                     
# printing result
print("Initial word separated list : " + str(res))


Output : 

The original string is : gfg is best
Initial word separated list : ['gfg', 'is best']

Time Complexity: O(N), where N is the size of the given string.
Auxiliary Space: O(N)

Method #3 : Using split() and join() methods

Python3




# Python3 code to demonstrate working of
# Separate first word from String
# Using split() and join()
 
# initializing string
test_str = "gfg is best"
 
# printing original string
print("The original string is : " + test_str)
 
# Separate first word from String
# Using split()
x=test_str.split()
x[0]=x[0]+"*"
p=" ".join(x)
res=p.split("*")
                     
# printing result
print("Initial word separated list : " + str(res))


Output

The original string is : gfg is best
Initial word separated list : ['gfg', ' is best']

Time Complexity: O(N), where N is the size of the given string.
Auxiliary Space: O(N)

Method#4: Using indexing

This Approach takes a string input, finds the first space in the string and splits the string into two parts: the first word and the rest of the string, and returns them as a list.

Algorithm

1. Take the input string
2. Use the index() method to find the index of the first whitespace character in the string.
3. Split the string into two parts using the index.
4. Append the first part and the remaining part to a list.
5. Return the list.

Python3




def separate_first_word(s):
    idx = s.index(" ")
    word_list = [s[:idx], s[idx+1:]]
    return word_list
 
s = "gfg is best"
result = separate_first_word(s)
print(result)


Output

['gfg', 'is best']

Time Complexity: O(n) where n is the length of the input string
Auxiliary Space: O(n) where n is the length of the input string

Method#5: Using itertools.takewhile() and itertools.dropwhile():

Algorithms:

  1. Define the input string ‘test_str’.
  2. Import the ‘itertools’ module.
  3. Use the ‘takewhile()’ method from itertools to take all characters until the first space is encountered.
    1. Use a lambda function to define the condition for taking characters from the string: ‘lambda x: x != ‘ ”.
    2. Convert the resulting iterator to a list using ‘list()’.
    3. Join the list of characters into a single string using ‘join()’.
    4. Store the resulting string in a variable ‘first_word’.
  4. Use the ‘dropwhile()’ method from itertools to drop all characters until the first space is encountered.
    a) Use a lambda function to define the condition for dropping characters from the string: ‘lambda x: x != ‘ ”.
    b) Convert the resulting iterator to a list using ‘list()’.
    c) Join the list of characters into a single string using ‘join()’.
    d) Store the resulting string in a variable ‘rest_of_string’.
  5. Remove any leading or trailing spaces from the ‘rest_of_string’ variable using ‘strip()’.
  6. Create a list with the ‘first_word’ and ‘rest_of_string’ variables.
  7. Print the list as the output.

Below is the implementation of the above approach:

Python3




import itertools
 
test_str = "gfg is best"
 
# Print original string
print("The original string is : " + test_str)
 
# Using takewhile() and dropwhile()
first_word = ''.join(list(itertools.takewhile(lambda x: x != ' ', test_str)))
rest_of_string = ''.join(list(itertools.dropwhile(lambda x: x != ' ', test_str)))
 
# Print result as list
print("Initial word separated list : " + str([first_word, rest_of_string.strip()]))
 
# This code is contributed by Jyothi pinjala.


Output

The original string is : gfg is best
Initial word separated list : ['gfg', 'is best']

Time Complexity: The time complexity of the ‘itertools’ method to separate the first word from a string is O(N), where N is the length of the input string. This is because the ‘takewhile()’ and ‘dropwhile()’ methods both iterate through the string once and the ‘join()’ method takes O(N) time to join the list of characters into a string.

Space Complexity: The space complexity of the ‘itertools’ method to separate the first word from a string is O(N), where N is the length of the input string. This is because we convert the resulting iterator from the ‘takewhile()’ and ‘dropwhile()’ methods to a list, which takes up additional memory.

Approach#6: Using regular expression

This approach uses regular expressions to extract the first word from the given string. The re.findall() method is used to find all non-overlapping matches of the regular expression r’^\S+|\s\S.*’ in the input string. This regular expression matches either a sequence of non-whitespace characters at the beginning of the string (^\S+) or a whitespace character followed by any number of non-whitespace characters (\s\S.*). This ensures that the first word is separated as a distinct element in the list, without including the leading whitespace character.

Algorithm

1. Define a lambda function that takes a string as input.
2. Use the re.findall() method to find all non-overlapping matches of the regular expression r’^\S+|\s\S.*’ in the input string.
3. Return the resulting list of matches.

Python3




import re
string = "gfg is best"
separated_list = (lambda s: re.findall(r'^\S+|\s\S.*', s))(string)
print(separated_list)


Output

['gfg', ' is best']

Time Complexity: O(n), where n is the length of the input string. The re.findall() method scans the input string once to find all matches of the regular expression, which takes linear time in the length of the string.

Space Complexity: O(m), where m is the maximum length of a match in the input string. The re.findall() method returns a list of matches, each of which is a string. The maximum length of a match is the length of the input string, which occurs when the regular expression matches the entire string as a single word. Therefore, the space complexity is O(n) in the worst case. However, in practice, the space complexity is likely to be much smaller than O(n) because the regular expression typically matches shorter substrings of the input string.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads