Python Articles

Page 26 of 855

Find the first repeated word in a string in Python using Dictionary

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 2K+ Views

In a given sentence, there may be a word which gets repeated before the sentence ends. In this Python program, we are going to find the first word that appears multiple times in a string using a dictionary approach. The logical steps we follow to get this result are: Split the given string into words separated by space Convert these words into a dictionary using collections.Counter Traverse the list of words and find the first word with frequency > 1 Using collections.Counter ...

Read More

Different messages in Tkinter - Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 522 Views

Tkinter is Python's built-in GUI toolkit that provides various message display options for user interactions and program state changes. The Message widget displays multi-line text with customizable formatting, while the messagebox module offers standard dialog boxes like confirmations, errors, and warnings. Custom Message Widget The Message widget displays text with customizable appearance ? import tkinter as tk main = tk.Tk() key = "The key to success is to focus on goals and not on obstacles" message = tk.Message(main, text=key) message.config(bg='lightblue', font=('Arial', 14, 'italic'), width=300) message.pack(padx=20, pady=20) main.mainloop() This creates a ...

Read More

Create a stopwatch using python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 1K+ Views

A stopwatch is used to measure the time interval between two events, usually in seconds to minutes. It has various usage like in sports or measuring the flow of heat, current etc in an industrial setup. Python can be used to create a stopwatch by using its tkinter library. This library provides GUI features to create a stopwatch showing the Start, Stop and Reset options. The key component of the program is using the label.after() method of tkinter. Syntax label.after(ms, function=None) Parameters: ms − Time in milliseconds function − Callback function to execute ...

Read More

Find all the patterns of "1(0+)1" in a given string using Python Regex

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 298 Views

In this tutorial, we will find all occurrences of the pattern "1(0+)1" in a string using Python's regex module. The re module provides powerful pattern matching capabilities for text processing. The pattern "1(0+)1" represents a literal string containing the number 1, followed by parentheses with "0+" inside, and ending with another 1. Example Input and Output Input: string = "Sample 1(0+)1 string with 1(0+)1 unnecessary patterns 1(0+)1" Output: Total number of pattern matches are 3 ['1(0+)1', '1(0+)1', '1(0+)1'] Algorithm Follow these steps to find the pattern: 1. Import the re ...

Read More

FuzzyWuzzy Python library

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 835 Views

In this tutorial, we are going to learn about the FuzzyWuzzy Python library. FuzzyWuzzy library is developed to compare strings and provides fuzzy string matching capabilities. While we have other modules like regex and difflib to compare strings, FuzzyWuzzy is unique in its approach. The methods from this library return a score out of 100 indicating how closely the strings match, instead of simple true/false or string results. Installation To work with the FuzzyWuzzy library, we need to install fuzzywuzzy and optionally python-Levenshtein for better performance − pip install fuzzywuzzy pip install python-Levenshtein The ...

Read More

Print anagrams together in Python using List and Dictionary

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 765 Views

In this tutorial, we will write a program to find and print anagrams together using list and dictionary. Anagrams are words formed by rearranging the letters of another word, like "listen" and "silent". Algorithm Here's our step-by-step approach ? 1. Initialize a list of strings. 2. Initialize an empty dictionary. 3. Iterate through the list of strings. 3.1. Sort the string and check if it is present in the dictionary as key or not. 3.1.1. If the sorted string is already present dictionary as a ...

Read More

Print first m multiples of n without using any loop in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 794 Views

In this tutorial, we will write a program to find the first m multiples of a number n without using loops. For example, if n = 4 and m = 3, the output should be 4, 8, 12 (three multiples of four). The main constraint is avoiding loops. We can use the range() function to achieve this without loops. The range() function returns a range object that generates a sequence of numbers. Syntax range(start, stop, step) Parameters start − Starting number of the range stop − Ending number (not included in the ...

Read More

Python program to count occurrences of a word in a string

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 4K+ Views

In this tutorial, we will write a program that counts the number of times a word occurs in a string. You are given a word and a string, and we need to calculate the frequency of the word in the string. Suppose we have a string "I am a programmer. I am a student." and the word "am". The program will return 2 as the word occurs two times in the string. Algorithm 1. Initialize the string and the word as two variables. 2. Split the string at spaces using the split() method to get a ...

Read More

Python program to find the IP Address of the client

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 61K+ Views

In this tutorial, we are going to find the IP address of the client using the socket module in Python. Every laptop, mobile, tablet, etc., has their unique IP address. We will find it by using the socket module. Let's see the steps to find out the IP address of a device. Algorithm Import the socket module. Get the hostname using the socket.gethostname() method and store it in a variable. Find the IP address by passing the hostname as an argument to the socket.gethostbyname() method and store it in a variable. Print the IP address. ...

Read More

Sort a list according to the Length of the Elements in Python program

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 6K+ Views

We have a list of strings and our goal is to sort the list based on the length of strings in the list. We have to arrange the strings in ascending order according to their lengths. We can do this using Python built-in method sort() or function sorted() along with a key. Let's take an example to see the output − Input: strings = ["hafeez", "aslan", "honey", "appi"] Output: ["appi", "aslan", "honey", "hafeez"] Using sorted() Function The sorted() function returns a new sorted list without modifying the original list. We pass len as ...

Read More
Showing 251–260 of 8,547 articles
« Prev 1 24 25 26 27 28 855 Next »
Advertisements