Find the first repeated word in a given string. 

In this program, we will take a string as input and find the first repeated word in a given string. 

Steps to solve the program
  1. Take a string as input and create an empty list.
  2. Add each word from the string to the empty list using for loop.
  3. If a word repeats then break the loop.
  4. Print the output.
				
					#Input string
str1 = "ab bc ca ab bd ca"
temp = []

for word in str1.split():
    if word in temp:
        print("First repeated word: ", word)
        break
    else:
        temp.append(word)
				
			

Output :

				
					First repeated word:  ab
				
			

split a string on the last occurrence of the delimiter. 

find the second most repeated word in a given string

Split a string on last occurrence of delimiter. 

In this program, we will take a string as input and split a string on last occurrence of delimiter.

Steps to solve the program
  1. Take a string as input.
  2. Split the string on the last occurrence of delimiter using rsplit().
  3. Print the output.
				
					#Input string
str1 = "l,e,a,r,n,I,n,g,p,y,t,h,o,n"

#Printing output
print(str1.rsplit(',', 1))
				
			

Output :

				
					['l,e,a,r,n,I,n,g,p,y,t,h,o', 'n']
				
			

count and display the vowels in a string

find the first repeated word in a given string. 

Count the vowels in a string

In this program, we will take a string as input and count the vowels in a string.

Steps to solve the program
  1. Take a string as input.
  2. Create a list containing all the vowels and create a count variable and assign its value equal to 0.
  3. Use for loop to iterate over each character from the string.
  4. If that character is present in the vowels add 1 to the count variable.
  5. Print the output.
				
					#Input string
string = "welcome to Sqatools"
vowels = ["A","E","I","O","U",
         "a","e","i","o","u"]
count = 0

for char in string:
    if char in vowels:
        count += 1

#Printing output
print(count)
				
			

Output :

				
					7
				
			

swap commas and dots in a string.

split a string on the last occurrence of the delimiter. 

Swap commas and dots in a string.

In this program, we will take a string as input and swap commas and dots in a string.

Steps to solve the program
  1. Take a string as input.
  2. Swap commas with dots in the string using replace().
  3. Print the output.
				
					#Input string
string = "sqa,tools.python"

#Replacing , by .
string.replace(",",".")
				
			

Output :

				
					'sqa.tools.python'

				
			

convert a string into a list of words.

count and display the vowels in a string

Convert a string into a list of words.

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

Steps to solve the program
  1. Take a string as input.
  2. Convert the string into a list of words using split().
  3. Print the output.
				
					#Input string
string = "learning python is fun"
list1 = string.split(" ")

#Printing output
print(list1)
				
			

Output :

				
					['learning', 'python', 'is', 'fun']
				
			

check whether a string contains all letters of the alphabet or not.

swap commas and dots in a string.

String contains all letters alphabet or not.

In this program, we will take a string as input and string contains all letters alphabet or not

Steps to solve the program
  1. Take a string as input.
  2. Import string library and create a set of all alphabets using string library.
  3. Check whether a string contains all letters of the alphabet or not.
  4. Print True if yes else False.
				
					#Importing string
import string
alphabet = set(string.ascii_lowercase)

#Input string
string = "abcdgjksoug"

#Printing output
print(set(string.lower()) >= alphabet)
				
			

Output :

				
					False
				
			

strip spaces from a string.

convert a string into a list of words.

Strip spaces from a string.

In this program, we will take a string as input and strip spaces from a string.

Steps to solve the program
  1. Take a string as input.
  2. Strip the spaces from a string using strip().
  3. Print the output.
				
					#Input string
string = "    sqaltoolspythonfun     "

#Printing output
print(string.strip())
				
			

Output :

				
					sqaltoolspythonfun
				
			

print the index of the character in a string.

check whether a string contains all letters of the alphabet or not.

Print the index of the character in a string.

In this program, we will take a string as input and print the index of the character in a string.

Steps to solve the program
  1. Take a string as input.
  2. Find the index of the given character using index().
  3. Print the output.
				
					#Input string
string = "Sqatools"

#Printing output
print("The index of q is: ",string.index("q"))
				
			

Output :

				
					The index of q is:  1
				
			

find all substring frequencies in a string.

strip spaces from a string.

Find all substring frequencies in a string

In this program, we will take a string as input and find all substring frequencies in a string.

Steps to solve the program
  1. Take a string as input and create an empty list.
  2. Using for loop with range() function find all the possible substring combinations from the given string and add them to the empty list.
  3. Create an empty dictionary.
  4. Use for loop to iterate over each combination of the substring from the list.
  5. Add the combination as the key and its occurrences in the list as its value in the dictionary.
  6. Print the output.
				
					#Input string
str1 = "abab"
test = []

for i in range(len(str1)):
    for j in range(i+1,len(str1)+1):
        test.append(str1[i:j])

dict1 = dict()
for val in test:
    dict1[val] = test.count(val)

#Printing output
print(dict1)
				
			

Output :

				
					{'a': 2, 'ab': 2, 'aba': 1, 'abab': 1, 'b': 2, 'ba': 1, 'bab': 1}
				
			

check if the substring is present in the string or not

print the index of the character in a string.

Python String | Check if substring is in string or not.

In this program, we will take a string as input and check if the substring is in string or not.

Steps to solve the program
  1. Take a string and substring as input.
  2. Convert them into two different lists using split() and create a count variable and assign its length equal to 0.
  3. Use for loop to check whether a word from the substring is in the given string.
  4. If yes, then add 1 to the count variable each time.
  5. In the end, if the value of the count variable is equal to the length of the list containing the substring print yes else print no.
				
					#Input strings
str1 = "I live in Pune"
str2 = "I live"

list1 = str1.split(" ")
list2 = str2.split(" ")
count = 0

for word in list2:
    if word in list1:
        count += 1
        
#Printing output
if count == len(list2):
    print("Yes")
else:
    print("No")
				
			

Output :

				
					Yes
				
			

generate a random binary string of a given length.

find all substring frequencies in a string.