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.

Vowels in each word of string show as dictionary

In this program, we will take a string as input and count vowels in each word in the given string show as dictionary output

Steps to solve the program
  1. Take a string as input.
  2. Convert the string of words into a list using split() method and create another string that contains all the vowels, also create an empty dictionary.
  3. Use For loop to iterate over every word in the list and count the number of vowels it contains.
  4. Add the word as key and its vowel count as the value in the dictionary.
  5. Repeat the process for each word.
  6. Print the output.
				
					#Input string
string= "We are Learning Python Codding"

#Splitting the string
list1 = string.split(" ")

#Creating vowels list
vowels = "aeiou"

#creating an empty dictionary
dictionary = dict()

for word in list1:
    count = 0
    for char in word:
        if char in vowels:
            count +=1
    dictionary[word] = count

#Printing output    
print(dictionary)
				
			

Output :

				
					{'We': 1, 'are': 2, 'Learning': 3,'Python': 1,'Codding': 2}
				
			

exchange the first and last character of each word from the given string.

repeat vowels 3 times and consonants 2 times.

Exchange first and last character of each word

In this program, we will take a string as input and exchange first and last character of each word from a given string. 

Steps to solve the program
  1. Take a string as input.
  2. Convert that string into a list using Split().
  3. Use For loop to iterate over each word in the list.
  4. Using Indexing exchange the first and the last character of each word.
  5. Replace the new word formed with the old word in the list.
  6. After iterating over the last element convert the list into the string using join().
  7. Print the output
				
					#Input string
string ="Its Online Learning"

#Creating list
list1 = string.split(" ")

#Loop for swapping characters
for word in list1:
    #Creating new word
    new_word = word[-1]+word[1:-1]+word[0]
    #Finding index of the word
    index = list1.index(word)
    #Replacing the word with new word
    list1[index] = new_word
    
#Joining list and printing output    
" ".join(list1)
				
			

Output :

				
					'stI enlinO gearninL'
				
			

swap the last character of a given string.

count vowels from each word in the given string show as dictionary

Swap the last character of a string

In this program, we will take a string as input and swap the last character of a string

Steps to solve the program
  1. Take a string as input.
  2. Swap the last character of a string using Indexing.
  3. Print the output.
				
					#input string
string = "SqaTool"

#Printing output
print(string[-1]+string[1:-1]+string[0])
				
			

Output :

				
					lqaTooS
				
			

replace the second occurrence of any char with the special character $.

exchange the first and last character of each word

Calculate the length of a string with loop logic

In this program, we will take a string as input and calculate the length of a string with loop logic

Steps to solve the proram
  1. Take a string as input and create a variable and assign its value equal to zero.
  2. Use For loop to iterate over each character of the string.
  3. After each iteration add 1 to the variable to find the length of the string.
  4. To verify our solution find the length of the string using built it function len().
  5. Print the output.
				
					#Input string
string = "im am learing python"
count = 0

for char in string:
    count +=1
    
#Printing output
print("Length of the string using loop logic: ", count)
print("Length of the string using len(): ", len(string))
				
			

Output :

				
					Length of the string using loop logic:  20
Length of the string using len():  20
				
			

most simultaneously repeated characters in the input string.

replace the second occurrence of any char with the special character $.

Longest and smallest word in the input string

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

Steps to solve the program
  1. Take a string as input.
  2. Convert the list into a list using split().
  3. From that list find the longest and smallest word using max() and min().
  4. Use the key as len to find them in the min and max functions.
  5. Print the output.
				
					#Input string
string = "we are learning python"
list1 = string.split(" ")

#printing output
print("Longest word: ", max(list1, key = len))
print("Smallest word: ", min(list1, key = len))
				
			

Output : When we will run above program we will get following output.

				
					Longest word:  learning
Smallest word:  we
				
			

test whether a passed letter is a vowel or consonant.

most simultaneously repeated characters in the input string.

The passed letter is a vowel or consonant

In this program, we will take a string as input and test whether a passed letter is a vowel or consonant

Steps to solve the program
  1. Take a string as input.
  2. Using For loop test whether the passed letter is a vowel or consonant.
  3. Print the output.
				
					#Input letter
letter = "aerv"

#Printing output
for char in letter:
    if char == "a" or char =="e" or char =="i"
    or char =="o" or char =="u":
        print(f"{char} is vowel")
    else:
        print(f"{char} is consonant")
				
			

Output:

				
					a is vowel
e is vowel
r is consonant
v is consonant
				
			

count occurrences of a substring in a string.

Find the longest and smallest word in the input string.

Count occurrences of a substring in a string.

In this program, we will take a string and substring as input and count occurrences of a substring in a string.

Steps to solve the program
  1. Take a string and substring as input.
  2. Count the occurrences of a substring in a string using count().
  3. Print the output.
				
					#Input string
string = "sqatoolspythonspy"
sub = "spy"

#Printng output
string.count("spy")
				
			

Output :

				
					2
				
			

reverse a string if it’s length is a multiple of 4.

test whether a passed letter is a vowel or consonant.