Smallest and largest word

Python Practice › String Programs › Program 24

Smallest and largest word

EasyString Programs · 24 of 106

Write a Python program to find the smallest and largest word in a string.

Input
"Learning is a part of life and we strive"
Output
a Learning

Approach

  1. min and max with key=len.

Solution

Python
words = "Learning is a part of life and we strive".split()print(min(words, key=len), max(words, key=len))
Output
a Learning

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Common words in two strings

Python Practice › String Programs › Program 23

Common words in two strings

EasyString Programs · 23 of 106

Write a Python program to find the words that appear in both strings, ignoring punctuation.

Input
"Very Good Morning, How are You" and "You are a Good student, keep it up"
Output
['Good', 'You', 'are']

Approach

  1. Strip punctuation, split into sets, and intersect.

Solution

Python
s1 = "Very Good Morning, How are You"s2 = "You are a Good student, keep it up"w1 = {w.strip(",.") for w in s1.split()}w2 = {w.strip(",.") for w in s2.split()}print(sorted(w1 & w2))
Output
['Good', 'You', 'are']

Line by line

Lines 3-4

strip(",.") removes punctuation stuck to a word, such as “Morning,”.

Line 5

& keeps the words present in both sets.

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Longest all-capital word

Python Practice › String Programs › Program 22

Longest all-capital word

EasyString Programs · 22 of 106

Write a Python program to find the longest word written in capital letters.

Input
"Learning PYTHON programming is FUN"
Output
PYTHON

Approach

  1. Filter words with isupper(), then take the longest.

Solution

Python
text = "Learning PYTHON programming is FUN"caps = [w for w in text.split() if w.isupper()]print(max(caps, key=len) if caps else None)
Output
PYTHON

Line by line

Line 3

The conditional expression avoids an error when no word is in capitals.

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Remove unwanted characters

Python Practice › String Programs › Program 21

Remove unwanted characters

EasyString Programs · 21 of 106

Write a Python program to remove every character that is not a letter or digit.

Input
"Prog^ra*m#ming", "Py(th)#@&on Pro$*#gram"
Output
Programming
PythonProgram

Approach

  1. Keep only alphanumeric characters.

Solution

Python
for text in ["Prog^ra*m#ming", "Py(th)#@&on Pro$*#gram"]:    print("".join(ch for ch in text if ch.isalnum()))
Output
Programming
PythonProgram

Line by line

Line 2

Spaces are not alphanumeric either, so “Py… Pro…” becomes one word, matching the expected output.

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Remove duplicate words

Python Practice › String Programs › Program 20

Remove duplicate words

EasyString Programs · 20 of 106

Write a Python program to remove duplicate words (ignoring case) and keep the first occurrence.

Input
"John jany sabi row john sabi"
Output
John jany sabi row

Approach

  1. Track lower-cased words already seen.

Solution

Python
text = "John jany sabi row john sabi"seen = set()result = []for w in text.split():    if w.lower() not in seen:        seen.add(w.lower())        result.append(w)print(" ".join(result))
Output
John jany sabi row

Line by line

Line 5

Comparing lower-cased words treats “John” and “john” as the same.

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Build a sentence from a list of words

Python Practice › String Programs › Program 19

Build a sentence from a list of words

EasyString Programs · 19 of 106

Write a Python program to create a string from a list of words.

Input
["There", "are", "Many", "Programming", "Language"]
Output
There are Many Programming Language

Approach

  1. " ".join(words).

Solution

Python
words = ["There", "are", "Many", "Programming", "Language"]print(" ".join(words))
Output
There are Many Programming Language

Line by line

Line 2

join puts the separator (a space) between the words.

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Palindrome words in a string

Python Practice › String Programs › Program 18

Palindrome words in a string

EasyString Programs · 18 of 106

Write a Python program to get all palindrome words from a string.

Input
"Python efe language aakaa hellolleh"
Output
['efe', 'aakaa', 'hellolleh']

Approach

  1. Keep words equal to their reverse.

Solution

Python
text = "Python efe language aakaa hellolleh"print([w for w in text.split() if w.lower() == w.lower()[::-1]])
Output
['efe', 'aakaa', 'hellolleh']

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Replace Java with Python, any case

Python Practice › String Programs › Program 17

Replace Java with Python, any case

EasyString Programs · 17 of 106

Write a Python program to replace the word “Java” (in any letter case) with “Python”.

Input
"JAVA is the Best Programming Language in the Market"
Output
Python is the Best Programming Language in the Market

Approach

  1. str.replace is case-sensitive, so use re.sub with re.IGNORECASE.

Solution

Python
import re text = "JAVA is the Best Programming Language in the Market"print(re.sub(r"\bjava\b", "Python", text, flags=re.IGNORECASE))
Output
Python is the Best Programming Language in the Market

Line by line

Line 4

\b matches whole words only, and the flag ignores case.

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Extract all numbers from a string

Python Practice › String Programs › Program 16

Extract all numbers from a string

EasyString Programs · 16 of 106

Write a Python program to get all the numbers from a multi-line string.

Input
a paragraph containing 1112, 1773, 444, 5324 and 555
Output
[1112, 1773, 444, 5324, 555]

Approach

  1. Use re.findall(r"\d+", text) and convert each match to int.

Solution

Python
import re text = """Sinak's 1112 aim is to 1773 create a new generation of people whounderstand 444 that an organization's 5324 success or failure isbased on 555 leadership excellence and not managerial acumen"""numbers = [int(n) for n in re.findall(r"\d+", text)]print(numbers)
Output
[1112, 1773, 444, 5324, 555]

Line by line

Line 6

\d+ matches each run of digits; the list comprehension converts them to integers.

Note

The original sample output listed 5324 twice and missed 444; the real numbers in the text are shown here.

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Reverse the order of words

Python Practice › String Programs › Program 15

Reverse the order of words

EasyString Programs · 15 of 106

Write a Python program to rearrange a sentence so its words appear in reverse order.

Input
"Cricket Plays Virat"
Output
Virat Plays Cricket

Approach

  1. Split, reverse the list, join.

Solution

Python
text = "Cricket Plays Virat"print(" ".join(text.split()[::-1]))
Output
Virat Plays Cricket

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs