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. 

Capitalize first and last letters of each word.

In this program, we will take a string as input and capitalize first and last letters of each word.

Steps to solve the program
  1. Take a string as input.
  2. Capitalize first and last letters of each word using Indexing and upper() function.
  3. Print the output.
				
					#Input string
string = "this is my first program"

for char in string.split(" "):
    #Printing output
    print(char[0].upper() + char[1:-1] + 
          char[-1].upper(), end=" ")
				
			

Output :

				
					ThiS IS MY FirsT PrograM 
				
			

remove spaces from a given string.

calculate the sum of digits of a given string.

Remove spaces from a given string

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

Steps to solve the program
  1. Take a string as input.
  2. Create an empty string.
  3. Add every character from the input string to the empty string except spaces.
  4. Print the output.
				
					#Input string
string1 = "python at sqatools"
str1 = ""

for char in string1:
    if char != " ":
        str1 += char

#Printing output
print(str1)
				
			

Output :

				
					pythonatsqatools
				
			

second most repeated word in a given string

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

Find the second most repeated word in a string.

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

Steps to solve the program
  1. Take a string as input and create an empty dictionary.
  2. Use for loop to iterate over each word of the string.
  3. Add a word as the key and its occurrences in a string as the value in the dictionary.
  4. Use sorted() and lambda function to sort the dictionary by values.
  5. Print the second most repeated word in a string.
				
					#Input string
string = "ab bc ac ab bd ac nk hj ac"
dictionary = dict()

for char in string.split(" "):
    if char != " ":
        dictionary[char] = string.count(char)

counts_ = sorted(dictionary.items(), key=lambda val: val[1])
#Printing output
print(counts_[-2])
				
			

Output :

				
					('ab', 2)
				
			

find the first repeated word in a given string. 

remove spaces from a given string.