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.

Reverse a string if its length is multiple of 4

In this program, we will take a string as input and reverse a string if its length is a multiple of 4

Steps to solve the program
  1. Take a list as input.
  2. If its length is divisible by 4 then reverse the string.
  3. Print the output.
				
					#Input string
string = "sqatools"

if len(string) % 4 == 0:
    print(string[::-1])
				
			

Output :

				
					slootaqs
				
			

string made of 4 copies of the last two characters

count occurrences of a substring in a string.

String made of 4 copies of last 2 characters

In this program, we will take a string as input and get a string made of 4 copies of last 2 characters

Steps to solve the program
  1. Take a string as input.
  2. Select the last 2 characters from the string using indexing.
  3. Multiply them by 4 to get 4 copies.
  4. Print the output
				
					#Input string
string = "Sqatools"

#printing output
print(string[-2:]*4)
				
			

Output :

				
					lslslsls
				
			

list of strings and returns the length of the longest string.

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