Python - Separate first word from String Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We need to write a Python program to split a given string into two parts at the Kᵗʰ occurrence of a specified character. If the character occurs fewer than K times, return the entire string as the first part and an empty string as the second part. Separating the first word from a string involves identifying and extracting the substring that appears before the first space or delimiter. This can be efficiently done using methods like split() or partition() which split the string at spaces or specific characters, allowing us to isolate the first word.Using split()split() method divides the string into words (by whitespace by default) and returns a list. The first element of the list will be the first word. Python s1 = "Hello, this is a sample string." # Split the string and get the first word s2 = s1.split()[0] print(s2) OutputHello, Explanation: split() by default, split() splits the string based on spaces, so it will create a list of words. The first word will be at index 0 of the list.Using partition()We can also use the partition() method, which splits the string into three parts: everything before the first occurrence of the separator (space), the separator itself, and everything after it. Python s1 = "Hello, this is a sample string." # Partition the string at the first space s2, _, _ = s1.partition(" ") print(s2) OutputHello, Explanation: partition() splits the string into three parts based on the first occurrence of the specified separator (in this case, a space). first part is the first word, which can be directly accessedUsing Regular Expressions (re)We can also use regular expressions to extract the first word in a more flexible way (especially if the definition of "word" is more complex). Python import re s1 = "Hello, this is a sample string." # Use regex to match the first word s2 = re.match(r'\S+', s1).group() print(s2) OutputHello, Explanation:re.match(r'\S+', s): This regex pattern \S+ matches the first non-space sequence of characters (i.e., the first word)..group(): Extracts the matched substring, which is the first word Comment More infoAdvertise with us Next Article Python String split() M manjeet_04 Follow Improve Article Tags : Python Python string-programs Practice Tags : python Similar Reads Reverse Words in a Given String in Python In this article, we explore various ways to reverse the words in a string using Python. From simple built-in methods to advanced techniques like recursion and stacks. We are going to see various techniques to reverse a string.Using split() and join()Using split() and join() is the most common method 2 min read Python String rpartition() Method String rpartition() method in Python is a powerful tool for splitting a string into three parts, based on the last occurrence of a specified separator. It provides an efficient way to handle text parsing when we want to divide strings from the rightmost delimiter. Let us start with a simple example: 4 min read Python String split() Python String split() method splits a string into a list of strings after breaking the given string by the specified separator.Example:Pythonstring = "one,two,three" words = string.split(',') print(words) Output:['one', 'two', 'three']Python String split() Method SyntaxSyntax: str.split(separator, m 6 min read Python Capitalize First Letter of Every Word in String There are different ways to capitalize the first letter of every word in a string using Python. We will cover methods such as using str.capitalize(), loops and regular expressions to achieve this text formatting task.Using str.title()str.title() method in Python converts the first character of each 3 min read Find the first repeated word in a string in Python using Dictionary We are given a string that may contain repeated words and the task is to find the first word that appears more than once. For example, in the string "Learn code learn fast", the word "learn" is the first repeated word. Let's understand different approaches to solve this problem using a dictionary. U 3 min read How to Substring a String in Python A String is a collection of characters arranged in a particular order. A portion of a string is known as a substring. For instance, suppose we have the string "GeeksForGeeks". In that case, some of its substrings are "Geeks", "For", "eeks", and so on. This article will discuss how to substring a str 4 min read Like