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.

Generate a random binary string of given length.

In this program, we will generate a random binary string of a given length.

Steps to solve the program
  1. Import the random library to generate random numbers and create an empty string.
  2. Using for loop with range() function and random.randint generates a random binary number of a given length and add it to the empty string.
  3. Print the output.
				
					#Importing random
import random
result = " "

for i in range(9):
    val = str(random.randint(0,1))
    result += val

#Printing output    
print(result)
				
			

Output :

Note: Output can be different since the numbers are selected randomly.

				
					 001110000
				
			

sort a string

check if the substring is present in the string or not

Python program to sort the string

In this program, we will take a string as input and sort the string.

Steps to solve the program
  1. Take a string as input.
  2. Sort the string using sorted().
  3. Print the output.
				
					#Input string
string="xyabkmp"

#Printing output
print("".join(sorted(string)))
				
			

Output :

				
					'abkmpxy'
				
			

check whether the string is a subset of another string or not

generate a random binary string of a given length.

String is a subset of another string or not

In this program, we will take a string as input and check whether the string is a subset of another string or not.

Steps to solve the program
  1. Take a string and a substring as input.
  2. Convert the input string to a set using set() and assign it to the string3 variable.
  3. Create a count variable and assign its value equal to 0.
  4. Use for loop to iterate over string3, if a character from string3 exists in the substring then add 1 to the count variable every time.
  5. If the value of count variable is equal to the length of the substring then print True else False.
				
					#Input strings
string1 = "iamlearningpythonatsqatools"
string2 = "pystlmi"

string3 = set(string1)
count = 0

for char in string3:
    if char in string2:
        count += 1
        
#Printing output
if count == len(string2):
    print(True)
else:
    print(False)
				
			

Output :

				
					True
				
			

find duplicate characters in a string

sort a string

Find duplicate characters in a string

In this program, we will take a string as input and find duplicate characters in a string

Steps to solve the program
  1. Take a string as input and create an empty list.
  2. Use for loop to iterate over every character of the string.
  3. Count the occurrences of each character in the string using count().
  4. If count > 1 then add that character to the empty list.
  5. Convert that list to a set.
  6. Print output
				
					#Input string
string = "hello world"
list1 = []

for char in string:
    if string.count(char) > 1:
        list1.append(char)

#Printing output
print(set(list1))
				
			

Output :

				
					{'o', 'l'}
				
			

remove punctuations from a string

check whether the string is a subset of another string or not

Remove punctuations from a string

In this program, we will take a string as input and remove punctuations from a string.

Steps to solve the program
  1. Take a string as input.
  2. Create an empty string and a string containing all the punctuations.
  3. Use for loop to iterate over every character from the input string.
  4. If the character is not in the list containing punctuations then add that character to the empty string.
  5. Print the output.
				
					#Input string
string1 = "Sqatools : is best, for python"
string2 = ""
punc = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

for char in string1:
    if char not in punc:
        string2 += char
        
#Printing output
print(string2)
				
			

Output :

				
					Sqatools  is best for python
				
			

remove empty spaces from a list of strings.

find duplicate characters in a string

Remove empty spaces from a list of strings.

In this program, we will take a list of strings as input and remove empty spaces from a list of strings.

Steps to solve the program
  1. Take a list of strings as input.
  2. Create an empty list.
  3. Add every string from the input list to the empty list except empty spaces.
  4. Print the output.
				
					#Input lists
List1 =  ["Python", " ", " ","sqatools"]
List2 = []

for string in List1:
    if string != " ":
        List2.append(string)

#Printing output
print(List2)
				
			

Output :

				
					['Python', 'sqatools']
				
			

replace different characters in the string at once.

remove punctuations from a string

Replace different characters in string at once.

In this program, we will take a string as input and replace different characters in string at once.

Steps to solve the program
  1. Take a string as input and create an empty string.
  2. Add each character from the given string to the empty string after replacing different characters.
  3. Use for loop for this purpose.
  4. Print the output.
				
					#Input string
string = "Sqatool python"
new_str = ""

for char in string:
    if char == "a":
        new_str += "1"
    elif char == "t":
        new_str += "2"
    elif char == "o":
        new_str += "3"
    else:
        new_str += char

#Printing output
print(new_str)
				
			

Output :

				
					Sq1233l py2h3n
				
			

replace multiple words with certain words.

remove empty spaces from a list of strings.