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

Floating numbers up to 3 decimal places.

In this program, we will take a number as input and print floating numbers up to 3 decimal places, and convert the number to a string.

Steps to solve the program
  1. Take a floating number as input.
  2. Create an empty string.
  3. Use the round() function to round up the given floating number up to 3 decimal places.
  4. Conver rounded number to a string using str() and add it to the empty string.
  5. Print thee output.
				
					#Input string
num = 2.14652
result = ""

#Rounding number
result += str(round(num, 3))

#Printing output
print(result)
				
			

Output :

				
					2.147
				
			

split and join a string

convert numeric words to numbers.

Split and join a string

In this program, we will take a string as input and split and join a string.

Steps to solve the program
  1. Take a string as input.
  2. Split the given string using Split().
  3. Join the splitter string using join() and ““.
  4. Print the output.
				
					#input string
string = "Hello world"

#Splitting the string
new_str = string.split(" ")
print(new_str)

#Joinin using -
print("-".join(new_str))
				
			

Output :

				
					['Hello', 'world']
Hello-world
				
			

remove a new line from a string

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

Remove a new line from a string

In this program, we will take a string as input and remove a new line from a string.

Steps to solve the program
  1. Take a string as input.
  2. Remove the new line from the string using strip().
  3. Print the output.
				
					#Input string
string1= "objectorientedprogramming\n"

#Printing output
print(string1.rstrip())
				
			

Output :

				
					objectorientedprogramming
				
			

convert all the characters in a string to Upper Case.

split and join a string

Convert all characters to Uppercase in a string.

In this program, we will take a string as input and convert all characters to Uppercase in a string

Steps to solve the program
  1. Take a string as input.
  2. Convert all characters of the given string to Uppercase using Upper().
  3. Print the output.
				
					#Input string
string1 = "I live in pune"

#Printing output
print(string1.upper())
				
			

Output :

				
					I LIVE IN PUNE
				
			

exchange the first and last letters of the string

remove a new line from a string in a python.

Exchange first & last letters of the string

In this program, we will take a string as input and exchange first & last letters of the string

Steps to solve the program
  1. Take a string as input.
  2. Exchange the first and last letters of the string using Indexing.
  3. Print the output.
				
					#Input string
string1= "We are learning python"

#Printing output
print(string1[-1]+string1[1:-1]+string1[0])
				
			

Output :

				
					ne are learning pythoW
				
			

check if a string has a special character or not

convert all the characters in a string to Upper Case.

Check if a string has special character or not

In this program, we will take a string as input and check if a string has special character or not.

Steps to solve the program
  1. Take a string as input.
  2. Create a string with all special characters as its value.
  3. Create a count variable and assign its value equal to zero.
  4. Use For loop to iterate over each character of the string.
  5. If the character is in a string of special characters then add 1 to the count variable.
  6. In the end, if the count variable is greater than 0 then the string has a special character.
  7. Print the output.
				
					#Input strings
string = "python$$#sqatools"

#Special characters
s ='[@_!#$%^&*()<>?/\|}{~:]'
count = 0

for char in string:
    if char in s:
        count += 1
        
#Printing output
if count > 0:
    print("Given string has special characters")
else:
    print("‘Given string does not has special characters")
				
			

Output :

				
					Given string has special characters
				
			

remove all duplicate characters from a given string

exchange the first and last letters of the string