Find the smallest and largest word from a string

In this program, we will take a string as input and find the smallest and largest word in a given string

Steps to solve the program
  1. Take a string as input.
  2. Split the string using Split().
  3. Find the smallest and the largest word from a string using min() and max().
				
					#Input string
string = "Learning is a part of life and we strive"

#Printing output
print("Longest words: ",max(string.split(" "),key=len))
print("Smallest word: ",min(string.split(" "),key=len))
				
			

Output :

				
					Longest words:  Learning
Smallest word:  a
				
			

get common words from strings.

whether the given string is a palindrome (similar) or not.

Get common words from strings

In this program, we will take two strings as input and get common words from strings.

Steps to solve the program
  1. Take two strings as input and create an empty list.
  2. Using For loop find the common words from strings and add them to the empty list.
  3. Convert the list into a string Join().
  4. Print the output.
				
					#Input strings
string1 = "Very Good Morning, How are You"
string2 = "You are a Good student, keep it up"
List = []

for word in string1.split(" "):
    if word in string2.split(" "):
        List.append(word)

#Printing output        
" ".join(List)
				
			

Output :

				
					'Good are You'
				
			

find the longest capital letter word from the string.

find the smallest and largest word in a given string.

Find the longest capital letter word from string

In this program, we will take a string as input and find the longest capital letter word from the string

Steps to solve the program
  1. Take a string as input.
  2. Import re library to find the all capital letter words.
  3. Find the longest capital letter word from the string using max(),
  4. Print the output.
				
					#Importing re
import re

#Input string
string="Learning PYTHON programming is FUN"

#Finding all capital letters words
word = re.findall(r"[A-Z]+", string)

#Printing output
print(max(word, key=len))
				
			

Output :

				
					PYTHON
				
			

remove unwanted characters from the given string.

get common words from strings.

Remove unwanted characters from the given string

In this program, we will take a string as input and remove unwanted characters from the given string.

Steps to solve the program
  1. Take a string as input.
  2. Remove the unwanted characters using isalnum() and for loop.
  3. Print the output.
				
					#Input string
string1="Prog^ra*m#ming"
test_str = ''.join(letter for letter in string1 if letter.isalnum())
#Printing output
print(test_str)

#Input string
string2="Py(th)#@&on Pro$*#gram"
test_str = "".join(letter for letter in string2 if letter.isalnum())
#Printing output
print(test_str)
				
			

Output :

				
					Programming

Programming PythonProgram
				
			

remove duplicate words from the string.

find the longest capital letter word from the string.

Remove duplicate words from the string.

In this program, we will take a string as input and remove duplicate words from the string.

Steps to solve the program
  1. Take a string as input.
  2. Convert the string into a list and create an empty list.
  3. Use for loop to iterate over every of the list.
  4. Add every of the list to the empty list, if a word repeats then do not add it to the empty list.
  5. Convert the new list into a string using join().
  6. Print the output.
				
					#Input string
string = "John jany sabi row John sabi"
list1 = string.split(" ")
list2 = []

for val in list1:
    if val not in list2:
        list2.append(val)
        
#Printing output
" ".join(list2)
				
			

Output :

				
					'John jany sabi row'
				
			

create a string with a given list of words.

remove unwanted characters from the given string.

Create a string from a list of words

In this program, we will take a list as input and create a string from a list of words.

Steps to solve the program
  1. Take a list as input.
  2. Convert the list into a string by using join().
  3. Print the output.
				
					#Input list
list1 = ["There", "are", "Many", "Programming", "Language"]

#Printing output
" ".join(list1)
				
			

Output :

				
					'There are Many Programming Language'
				
			

get all the palindrome words from the string.

remove duplicate words from the string.

Get all the palindrome words from the string

In this program, we will take a string as input and get all the palindrome words from the string

Steps to solve the program
  1. Take a string as input.
  2. Convert the string into a list using Split() and create an empty list.
  3. Use For loop to iterate over every of the list.
  4. Check if the word is palindrome or not, if yes then add that word to the empty list.
  5. Print the new list.
				
					#Input string
string = "Python efe language aakaa hellolleh"
List = string.split(" ")
new_list = []

for val in List:
    if val == val[::-1]:
        new_list.append(val)

#Printing output
print(new_list)
				
			

Output :

				
					['efe', 'aakaa', 'hellolleh']
				
			

replace the words “Java” with “Python” in the given string.

create a string with a given list of words.

Replace the words in the given string

In this program, we will take a string as input and replace the words “Java” with “Python” in the given string

Steps to solve the program
  1. Take a string as input.
  2. Convert the list into a list.
  3. Use For loop to iterate over each word in the string.
  4. If a word is equal to “JAVA” replace the word with “PYTHON”.
  5. combine the new list using join().
  6. Print the output.
				
					#Input string
string = "JAVA is the Best Programming Language in the Market"
List1 = string.split(" ")

for word in List1:
    if word == "JAVA":
        index = List1.index(word)
        List1[index] = "PYTHON"

#Printing output
print(" ".join(List1))
				
			

Output :

				
					PYTHON is the Best Programming Language in the Market
				
			

get all the digits from the given string.

get all the palindrome words from the string.

Get all the digits from the string.

In this program, we will take a string as input and get all the digits from the string.

Steps to solve the program
  1. Take a string as input
  2. Convert the string into a list using Split() and create an empty list.
  3. Use For loop to iterate over every word in the string.
  4. If the words contain all digits add the words to the empty list.
  5. Use isdigit() for this purpose.
  6. Print the new list.
				
					#Input string
string1 = """"Sinak’s 1112 aim is to 1773 create a new generation of people who understand 444 that an organization’s 
        5324 success or failure is based on 555 leadership excellence and not managerial acumen"""

List1 = string1.split(" ")
List2 = []

for val in List1:
    if val.isdigit():
        List2.append(val)

#Printing output
print(List2)
				
			

Output :

				
					['1112', '1773', '444', '5324', '555']
				
			

re-arrange the string.

replace the words “Java” with “Python” in the given string.

Re-arrange the string

In this program, we will take a string as input and re-arrange the string.

Steps to solve the program
  1. Take a string as input.
  2. Convert the string into a list using Split().
  3. Arrange the string in the required manner.
  4. Convert the list into a string using join().
  5. Print the output.
				
					#Input string
string = "Cricket Plays Virat"

#Converting to a list
List = string.split(" ")
List.reverse()

#Printing output
" ".join(List)
				
			

Output :

				
					'Virat Plays Cricket'
				
			

repeat vowels 3 times and consonants 2 times.

get all the digits from the given string.