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.

Replace multiple words with certain words.

In this program, we will take a string as input and replace multiple words with certain words.

Steps to solve the program
  1. Take a string as input.
  2. Convert the string of words into a list of words using split().
  3. Use for loop with range() function to iterate over each word in the list.
  4. Replace the word in the string with a defined word.
  5. Convert the list into a string using join().
  6. Print the output.
				
					#Input string
string = "I’m learning python at Sqatools"
List = string.split(" ")

for i in range(len(List)):
    if List[i] == "python":
        List[i] = "SQA"
    elif List[i] == "Sqatools":
        List[i] = "TOOLS"

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

Output :

				
					I’m learning SQA at TOOLS
				
			

split strings on vowels

replace different characters in the string at once.

Split strings on vowels

In this program, we will take a string as input and split strings on vowels.

Steps to solve the program
  1. Take a string as input and import re library.
  2. Split the string on vowels using re.split().
  3. Print the output.
				
					#Importing re library
import re

#Input string
string = "qwerty"

#Splitting string on vowels
result = re.split('a|e|i|o|u', string)

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

Output :

				
					qw rty
				
			

print the mirror image of the string.

replace multiple words with certain words.

Print the mirror image of string.

In this program, we will take a string as input and print the mirror image of string.

Steps to solve the program
  1. Take a string as input.
  2. Print the mirror image of the string by reversing it.
  3. Use Indexing for this purpose.
				
					#Input string
string = "Python"

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

Output :

				
					'nohtyP'
				
			

get a string made of the first 2 and the last 2 characters

split strings on vowels