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

Get a string made of the first and last 2 chars

In this program, we will take a string as input and get a string made of the first and last 2 chars

Steps to solve the program
  1. Take a list as input.
  2. Print the new string made of the first and last 2 characters from the given string using Indexing.
				
					#Input string
string = "Sqatools"

#Printing output
print(string[:2]+string[-2:])
				
			

Output :

				
					Sqls
				
			

get the first 4 characters of a string.

print the mirror image of the string.

Get the first 4 characters of a string

In this program, we will take a string as input and get the first 4 characters of a string.

Steps to solve the program
  1. Take a string as input.
  2. Print the first 4 characters of a string using Indexing.
				
					#Input string
string = "Sqatools"

#Printing output
print(string[:4])
				
			

Output :

				
					Sqat
				
			

words greater than the given length.

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

Find the words greater than the given length.

In this program, we will take a string as input and find words greater than the given length.

Steps to solve the program
  1. Take a string as input.
  2. Split the given string using split().
  3. Find the words having lengths greater than 3 from the splitter string using For loop.
  4. Print the output.
				
					#Input string
string = "We are learning python"

for word in string.split(" "):
    if len(word) > 3:
        print(word,end=" ")
				
			

Output :

				
					learning python
				
			

find the least frequent character in a string.

get the first 4 characters of a string.

Repeat vowels and consonants 3 and 2 times resp.

In this program, we will take a string as input and repeat vowels and consonants 3 and 2 times respectively.

Steps to solve the program
  1. Take a string as input.
  2. Create an empty string and a list containing all the vowels.
  3. Using for loop check whether a character from the input string is a vowel or consonant.
  4. If it is a vowel then add it to the empty list 3 times each.
  5. If it is a consonant add it to the empty list 2 times each.
  6. Print the output
				
					#Input string
str1  = "Sqa Tools Learning"
result = ""
vowels = ["a","e","i","o","u",
          "A","E","I","O","U"]

for char in str1:
    if char in vowels:
        result = result + char*3
    else:
        result = result + char*2

#Printing output
print(result)
				
			

Output :

				
					SSqqaaa  TToooooollss  LLeeeaaarrnniiinngg
				
			

count vowels from each word in the given string show as dictionary

re-arrange the string.

Replace the second occurrence of a character

In this program, we will take a string as input and replace the second occurrence of any char with the special character $.

Steps to solve the program
  1. Take a string as input and create an empty string.
  2. Using For loop add each character of the given string to the empty string.
  3. If a character repeats then do not add that character add $ instead.
  4. Print the output.
				
					#Input string
str1 = "Programming"
result = ''

#checking for repeated char
for char in str1:
    if char in result:
        result = result + "$"
    else:
        result = result + char

#Printing output
print("Result :", result)
				
			

Output :

				
					Result : Prog$am$in$
				
			

calculate the length of a string with loop logic.

swap the last character of a given string.

Most simultaneously repeated character in string

In this program, we will take a string as input and find the most simultaneously repeated character in the input string.

Steps to solve the program
  1. Take a string as input.
  2. Create two variables to find the most simultaneously repeated character in a string.
  3. Using For loop with the range() function find whether a character is repeated in the string or not.
  4. If yes then, count how many times it has been repeated.
  5. Print the most simultaneously repeated character in a string.
				
					#Input string
str1 = "Helllllo ffdfdas sdfsfsd sssfdddd"
max_repeat_count = 0
max_repeat_char = ''

temp = 1
for i in range(len(str1)-1):
    if str1[i] == str1[i+1]:
        temp = temp + 1
        if temp > max_repeat_count:
            max_repeat_count = temp
            max_repeat_char = str1[i]
    else:
        temp = 1

#Printing output
print("Max repeated char :", max_repeat_char,
      "\nMax repeated count :", max_repeat_count)
				
			

Output :

				
					Max repeated char : l 
Max repeated count : 5
				
			

Find the longest and smallest word in the input string.

calculate the length of a string with loop logic.