Remove unwanted characters from a string.

In this program, we will take a string as input and remove unwanted characters from a given string.

Steps to remove the program
  1. Take a string as input.
  2. Create a list of unwanted characters.
  3. Using for loop check whether each character from the string belongs in the list of unwanted characters.
  4. If yes then remove the unwanted character.
  5. Print the output.
				
					#Input string
string = "sqa****too^^{ls"
unwanted = ["#", "*", "!", "^", "%","{","}"]

for char in unwanted:
    string = string.replace(char,"")

#Printing output
print(string)
				
			

Output :

				
					sqatools
				
			

count a number of non-empty substrings of a given string.

find the string similarity between two given strings

Count number of non-empty substrings of string.

In this program, we will take a string as input and count a number of non-empty substrings of a given string.

Steps to remove the program
  1. Take a string as input.
  2. Count the number of non-empty substrings using the following formula- n*n+1/2  where n is the length of the string.
  3. Print the output.
				
					#Input string
str1 = "sqatools12"

result = int(len(str1) * (len(str1) + 1) / 2)

#Printing output
print(result)
				
			

Output :

				
					55
				
			

count all the Uppercase, Lowercase, special character and numeric values in a given string

remove unwanted characters from a given string

Count all the values in a string.

In this program, we will take a string as input and count all the Uppercase, Lowercase, special character and numeric values in a given string.

Steps to solve the program
  1. Take a string as input.
  2. Create some variables and assign their values equal to 0.
  3. Use for loop to iterate over each character of the string.
  4. Check the type of the character and add 1 to the respective variable.
  5. Print the output.
				
					#Input string
str1 = "@SqaTools#lin"
upper = 0
lower = 0
digit = 0
symbol = 0

for val in str1:
    if val.isupper():
        upper += 1  
    elif val.islower():
        lower += 1
    elif val.isdigit():
        digit += 1
    else:
        symbol += 1
        
#Printing output
print("Uppercase: ", upper)
print("Lowercase: ", lower)
print("Digits: ", digit)
print("Special characters: ", symbol)
				
			

Output :

				
					Uppercase:  2
Lowercase:  9
Digits:  0
Special characters:  2
				
			

remove all characters except the given character in a string. 

count a number of non-empty substrings of a given string.

Remove all characters except given character 

In this program, we will take a string as input and remove all characters except the given character in a string. 

Steps to solve the program
  1. Take a string as input.
  2. Remove all characters except the given character and add them to the list using list comprehension.
  3. Use for loop to iterate over each character of the string.
  4. Print the output.
				
					#Input string
str1 = "Sqatools python"

#Printing output
print(''.join([el for el in str1 if el == "S"]))
				
			

Output :

				
					S
				
			

create a string from two given strings combining uncommon characters of the said strings.  

Count all the Uppercase, Lowercase, special character and numeric values in a given string

Create a string from two strings

In this program, we will take two strings as input and create a string from two strings combining uncommon characters of the said strings. 

Steps to solve the program
  1. Take two strings as input.
  2. Create an empty string.
  3. Using for loop add characters from string 1 to the empty string which is not in string 2.
  4. Repeat this process for string 2.
  5. Print the output.
				
					#Input string
str1 = "abcdefg"
str2 = "xyzabcd"
str3 = ""

for char in str1:
    if char not in str2:
        str3 += char
for char in str2:
    if char not in str1:
        str3 += char

#Printing output        
print(str3)
				
			

Output :

				
					efgxyz
				
			

Create a string that consists of multi-time occurring characters in the said string

remove all characters except the given character in a string. 

String that consists multi-time occurring chars

In this program, we will take a string as input and create a string that consists of multi-time occurring characters in the said string.

Steps to solve the problem
  1. Take a string as input.
  2. From collections import counter to count the occurrences of the characters in the given string.
  3. Print only those characters who have occurred more than once.
				
					#Input string
str1 = "aabbcceffgh"

#Importing counter
from collections import Counter   
str_char = Counter(str1)

part1 = [ key for (key,count) in str_char.items() if count>1] 

#printing output
print("Occurring more than once: ", "".join(part1))
				
			

Output :

				
					Occurring more than once:  abcf
				
			

remove all consecutive duplicates of a given string

create a string from two given strings combining uncommon characters of the said strings.  

Remove all consecutive duplicates from a string.

In this program, we will take a string as input and remove all consecutive duplicates from a string

Steps to solve the program
  1. Take a string as input and create an empty list.
  2. From itertools import groupby for removing all consecutive duplicates from the given string.
  3. After removing consecutive duplicate characters add them to the list.
  4. Join the list ti convert it into a string using join().
  5. Print the output.
				
					#Input string
str1= "xxxxyy"
list1 = []

#Importing group
from itertools import groupby

for (key,group) in groupby(str1):
    list1.append(key)
    
#Printing output
print("".join(list1))
				
			

Output :

				
					xy
				
			

find the maximum length of consecutive 0’s in a given binary string

Create a string that consists of multi-time occurring characters

Find the maximum length of consecutive 0’s.

In this program, we will take a string as input and find the maximum length of consecutive 0’s in a given binary string.

Steps to solve the program
  1. Take a binary string as input.
  2. Split the binary string on 1 to find the maximum length of consecutive 0’s.
  3. Print the output.
				
					#Input string
string ="10001100000111"

#splitting on 1
list1 = string.split("1")
max_ = (max(list1,key=len))

#Printing output
print(len(max_))
				
			

Output :

				
					5
				
			

remove zeros from an IP address. 

remove all consecutive duplicates of a given string

Remove zeros from an IP address. 

In this program, we will take a string of IP address as input and remove zeros from an IP address. 

Steps to solve the program
  1. Take a string of an IP address as input.
  2. Import re library.
  3. Remove zeros from the IP address using re.sub().
  4. Print the output.
				
					#Importing re
import re

#Input string
str1 = "289.03.02.054"

string = re.sub('\.[0]*', '.', str1)

#Printing output
print(string)
				
			

Output :

				
					289.3.2.54
				
			

calculate the sum of digits of a given string.

find the maximum length of consecutive 0’s in a given binary string

Calculate the sum of digits in a given string.

In this program, we will take a string as input and compute the sum of digits in a given string.

Steps to solve the program
  1. Take a string as input.
  2. Creat a variable named total and assign its value equal to 0.
  3. Use for loop to iterate over each character of the input string.
  4. Check if the character is a number or nor using isnumeric()
  5. if it is a number then convert it into an integer and add it to the total variable.
  6. Print the output
				
					#Input string
string = "12sqatools78"
total = 0

for char in string:
    if char.isnumeric():
        total += int(char)

#Printing output        
print("Sum: ", total)
				
			

Output :

				
					Sum:  18
				
			

capitalize the first and last letters of each word of a given string.

remove zeros from an IP address.