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.

Find the least frequent character in a string.

In this program, we will take a string as input and find the least frequent character in a 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 character of the string.
  3. Add the character as the key and its occurrences in the string as its value in the dictionary.
  4. Find first the character having the least frequency from the value of the keys using min().
  5. Print the output.
				
					#Input string
string = "abcdabdggfhf"
char_freq = {}


for char in string:
    if char in char_freq:
        char_freq[char] = char_freq[char]+1
    else:
        char_freq[char] = 1
        
result = min(char_freq, key = char_freq.get)

#Printing output
print("Least frequent character: ",result)
				
			

Output :

				
					Least frequent character:  c
				
			

count occurrences of a word in a string.

Find the words greater than the given length.

Count occurrences of word in a string.

In this program, we will take a string as input and count occurrences of word in a string.

Steps to solve the program
  1. Take a string as input.
  2. Count the occurrences of a specified word in a string using count().
  3. Print the output.
				
					#Input string
string1 = "I want to eat fast food"

#Printing output
print("Occurences of food: ", 
      string1.count("food"))

#Input string
string2 = "We are learning Python, wow are you"
#Printing output
print("Occurences of we: ", 
      string2.count("are"))
				
			

Output :

				
					Occurences of food:  1
Occurences of we:  2
				
			

find the location of a word in a string

find the least frequent character in a string.

Find the location of a word in a string

In this program, we will take a string as input and find the location of a word in a string.

Steps to solve the program
  1. Take a string as input.
  2. Spit the string using Split() to convert it into a list.
  3. From the list use index() function to find the index of a word in the given string.
  4. Print the output.
				
					#Input string 
string = "I am solving problems based on strings"

#Splitting the string
List = string.split(" ")

#Printing output
List.index("problems")
				
			

Output : 

				
					3
				
			

convert numeric words to numbers.

count occurrences of a word in a string.

Convert numeric words to numbers

In this program, we will take a string as input and convert numeric words to numbers.

Steps to solve the program
  1. Take a string of numeric words as input.
  2. Use For loop to iterate over each word of the string.
  3. Create an empty string.
  4. Convert the numeric word to its corresponding number and add it to the empty string.
  5. Print the output.
				
					#Input string
string = "five four three two one"
new_str = ""

for val in string.split():
    if val == "one":
        new_str += "1"
    elif val == "two":
        new_str += "2"
    elif val == "three":
        new_str += "3"
    elif val == "four":
        new_str += "4"
    elif val == "five":
        new_str += "5"
    elif val == "six":
        new_str += "6"
    elif val == "seven":
        new_str += "7"
    elif val == "eight":
        new_str += "8"
    elif val == "nine":
        new_str += "9"
    elif val == "ten":
        new_str += "10"

#Printing output        
print(new_str)
				
			

Output :

				
					54321
				
			

floating numbers up to 3 decimal places and convert it to string.

find the location of a word in a string