Write a program that reads a series of strings and prints only those beginning with the letters “Th” in python

Write a program that reads a series of strings and prints only those beginning with the letters "Th" in python

This Python program reads a series of strings from the user, separates them by spaces, and then filters and prints only the strings that begin with the letters “Th”. The program uses the startswith() method to check if each string starts with the desired letters and prints them accordingly.

Python
def filter_strings(strings):
    """
    This function filters and prints only the strings that begin with the letters "Th".

    Args:
        strings (list): A list of strings.

    Returns:
        None
    """
    for string in strings:
        if string.startswith("Th"):  # Check if the string starts with "Th"
            print(string)

# Read the series of strings from the user
input_strings = input("Enter a series of strings (separated by spaces): ").split()

# Call the function to filter and print the strings starting with "Th"
filter_strings(input_strings)

The filter_strings() function takes a list of strings as input and iterates over each string in the list. For each string, it checks if the string starts with “Th” using the startswith() method. If the condition is true, meaning the string starts with “Th”, it is printed.

In the program, the user is prompted to enter a series of strings, which are then split and stored in the input_strings list. The filter_strings() function is called with the input_strings list as an argument, and it filters and prints the strings that begin with “Th” from the list.

Total
0
Shares

Leave a Reply

Previous Post
Program to the number count of letters in a given text in python

Program to the number count of letters in a given text in python

Next Post
Google Dorks: The Ultimate Guide for Hackers and Security Professionals

Google Dorks: The Ultimate Guide for Hackers and Security Professionals

Related Posts