Python Program to Print the first letter of each word using regex


Regex library in Python is used for pattern matching and manipulation of text data. We can print the first letter of each word using regex by identifying a new word after spaces using its pattern-matching functionality. In this article, we will implement a program to print the first letter of each word using regex.

Regular Expressions

Regular expression or regex is a tool for pattern matching in text. They are a sequence of characters that define a search pattern. They are widely used in programming, particularly in text processing, and are supported by most programming languages, including Python.

Printing the first letter of each word using regex

Method 1: Using findall() method

To print the first letter of each word using regex we need to first import the re module and create a function called first_letter which takes a string as an argument. In the first_letter function, we use the re.findall() method to find all the words in the string. The regex pattern ’\b\w’ is used to find the first character of each word. The '\b' is a word boundary, which matches the position between a word character and a non-word character. The '\w' matches any word character (letters, digits, or underscores).

The re.findall() method returns a list of all the characters of the words in the string. We then join the list of characters using join() method.

Syntax

re.findall(pattern, string, flags=0)

Here, the 'findall()' method returns all non-overlapping matches of the regex pattern in the string. The method takes three arguments: the regex pattern, the string to search, and optional flags. It returns a list of all the matches.

string.join(iterable)

Here, the 'join()' method concatenates the elements of an iterable (e.g., list, tuple, string) into a single string, using the specified string as a separator between each element. The method takes a single argument: the iterable to join.

re.finditer(pattern, string, flags=0)

Here, the 'finditer()' method returns an iterator of match objects for all non-overlapping matches of the regex pattern in the string. The method takes three arguments: the regex pattern, the string to search, and optional flags. It returns an iterator of match objects, which can be used to extract the matched strings.

re.split(pattern, string, maxsplit=0, flags=0)

Here, the 'split()' method splits the string into a list of substrings using the regex pattern as the delimiter. The method takes four arguments: the regex pattern, the string to split, the maximum number of splits (default is 0, meaning all possible splits), and optional flags. It returns a list of substrings.

Example 1

In the below example, we create a string “Python is a popular programming language” and passed it to the first_letter function. The function then returns the first letter of each word and then we can join the returned character using the join() method and print the output.

import re

def first_letter(string):
   words = re.findall(r'\b\w', string)
   return "".join(words)

string = "Python is a popular programming language"
result = first_letter(string)
print(result)

Output

Piappl

Example 2

In the below example, we first use the 're.split()' method to split the string into a list of words using '\W+' as the delimiter. '\W' matches any non-word character and '+' specifies one or more occurrences. We also add a filter to remove any empty strings from the list.Next, we use a list comprehension to extract the first character of each word and return it as a list. Finally, we use the 'str.join()' method to join the list of characters back into a string.

import re

def first_letter(string):
   return ''.join([word[0] for word in re.split('\W+', string) if word])
    
string = "Python is a popular programming language"
result = first_letter(string)
print(result)

Output

Piappl

Example 3

In the below example, we use the 're.finditer()' method to find all occurrences of the regex pattern '\b\w' in the string. We then iterate over each match and append the first character to a result string.

import re

def first_letter(string):
   result = ""
   for match in re.finditer(r'\b\w', string):
      result += match.group()
   return result

string = "Python is a popular programming language"
result = first_letter(string)
print(result)

Output

Piappl

Example 4

In the below example, we use the 're.split()' method to split the string into a list of words and delimiters. The regex pattern '(\W+)' matches one or more occurrences of any non-word character '\W'. The parentheses capture the delimiter into a separate item in the list.We then use a list comprehension to extract the first character of each word and return it as a list. Finally, we use the 'str.join()' method to join the list of characters back into a string.

import re

def first_letter(string):
   return ''.join([word[0] for word in re.split(r'(\W+)', string) if word])

string = "Python is a popular programming language"
result = first_letter(string)
print(result)

Output

P i a p p l

Conclusion

In this article, we discussed how we can print the first letter of each word using regex. Regex is a powerful tool for pattern matching in text data. To print the first letter of each word we used the re.findall() method to find the first character of the word in the string and then join each character using the join() function.

Updated on: 11-Jul-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements