Remove unwanted characters from the given string

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

Steps to solve the program
  1. Take a string as input.
  2. Remove the unwanted characters using isalnum() and for loop.
  3. Print the output.
				
					#Input string
string1="Prog^ra*m#ming"
test_str = ''.join(letter for letter in string1 if letter.isalnum())
#Printing output
print(test_str)

#Input string
string2="Py(th)#@&on Pro$*#gram"
test_str = "".join(letter for letter in string2 if letter.isalnum())
#Printing output
print(test_str)
				
			

Output :

				
					Programming

Programming PythonProgram
				
			

remove duplicate words from the string.

find the longest capital letter word from the string.

Remove duplicate words from the string.

In this program, we will take a string as input and remove duplicate words from the string.

Steps to solve the program
  1. Take a string as input.
  2. Convert the string into a list and create an empty list.
  3. Use for loop to iterate over every of the list.
  4. Add every of the list to the empty list, if a word repeats then do not add it to the empty list.
  5. Convert the new list into a string using join().
  6. Print the output.
				
					#Input string
string = "John jany sabi row John sabi"
list1 = string.split(" ")
list2 = []

for val in list1:
    if val not in list2:
        list2.append(val)
        
#Printing output
" ".join(list2)
				
			

Output :

				
					'John jany sabi row'
				
			

create a string with a given list of words.

remove unwanted characters from the given string.

Create a string from a list of words

In this program, we will take a list as input and create a string from a list of words.

Steps to solve the program
  1. Take a list as input.
  2. Convert the list into a string by using join().
  3. Print the output.
				
					#Input list
list1 = ["There", "are", "Many", "Programming", "Language"]

#Printing output
" ".join(list1)
				
			

Output :

				
					'There are Many Programming Language'
				
			

get all the palindrome words from the string.

remove duplicate words from the string.

Get all the palindrome words from the string

In this program, we will take a string as input and get all the palindrome words from the string

Steps to solve the program
  1. Take a string as input.
  2. Convert the string into a list using Split() and create an empty list.
  3. Use For loop to iterate over every of the list.
  4. Check if the word is palindrome or not, if yes then add that word to the empty list.
  5. Print the new list.
				
					#Input string
string = "Python efe language aakaa hellolleh"
List = string.split(" ")
new_list = []

for val in List:
    if val == val[::-1]:
        new_list.append(val)

#Printing output
print(new_list)
				
			

Output :

				
					['efe', 'aakaa', 'hellolleh']
				
			

replace the words “Java” with “Python” in the given string.

create a string with a given list of words.

Replace the words in the given string

In this program, we will take a string as input and replace the words “Java” with “Python” in the given string

Steps to solve the program
  1. Take a string as input.
  2. Convert the list into a list.
  3. Use For loop to iterate over each word in the string.
  4. If a word is equal to “JAVA” replace the word with “PYTHON”.
  5. combine the new list using join().
  6. Print the output.
				
					#Input string
string = "JAVA is the Best Programming Language in the Market"
List1 = string.split(" ")

for word in List1:
    if word == "JAVA":
        index = List1.index(word)
        List1[index] = "PYTHON"

#Printing output
print(" ".join(List1))
				
			

Output :

				
					PYTHON is the Best Programming Language in the Market
				
			

get all the digits from the given string.

get all the palindrome words from the string.

Get all the digits from the string.

In this program, we will take a string as input and get all the digits from the string.

Steps to solve the program
  1. Take a string as input
  2. Convert the string into a list using Split() and create an empty list.
  3. Use For loop to iterate over every word in the string.
  4. If the words contain all digits add the words to the empty list.
  5. Use isdigit() for this purpose.
  6. Print the new list.
				
					#Input string
string1 = """"Sinak’s 1112 aim is to 1773 create a new generation of people who understand 444 that an organization’s 
        5324 success or failure is based on 555 leadership excellence and not managerial acumen"""

List1 = string1.split(" ")
List2 = []

for val in List1:
    if val.isdigit():
        List2.append(val)

#Printing output
print(List2)
				
			

Output :

				
					['1112', '1773', '444', '5324', '555']
				
			

re-arrange the string.

replace the words “Java” with “Python” in the given string.

Re-arrange the string

In this program, we will take a string as input and re-arrange the string.

Steps to solve the program
  1. Take a string as input.
  2. Convert the string into a list using Split().
  3. Arrange the string in the required manner.
  4. Convert the list into a string using join().
  5. Print the output.
				
					#Input string
string = "Cricket Plays Virat"

#Converting to a list
List = string.split(" ")
List.reverse()

#Printing output
" ".join(List)
				
			

Output :

				
					'Virat Plays Cricket'
				
			

repeat vowels 3 times and consonants 2 times.

get all the digits from the given string.

Vowels in each word of string show as dictionary

In this program, we will take a string as input and count vowels in each word in the given string show as dictionary output

Steps to solve the program
  1. Take a string as input.
  2. Convert the string of words into a list using split() method and create another string that contains all the vowels, also create an empty dictionary.
  3. Use For loop to iterate over every word in the list and count the number of vowels it contains.
  4. Add the word as key and its vowel count as the value in the dictionary.
  5. Repeat the process for each word.
  6. Print the output.
				
					#Input string
string= "We are Learning Python Codding"

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

#Creating vowels list
vowels = "aeiou"

#creating an empty dictionary
dictionary = dict()

for word in list1:
    count = 0
    for char in word:
        if char in vowels:
            count +=1
    dictionary[word] = count

#Printing output    
print(dictionary)
				
			

Output :

				
					{'We': 1, 'are': 2, 'Learning': 3,'Python': 1,'Codding': 2}
				
			

exchange the first and last character of each word from the given string.

repeat vowels 3 times and consonants 2 times.

Exchange first and last character of each word

In this program, we will take a string as input and exchange first and last character of each word from a given string. 

Steps to solve the program
  1. Take a string as input.
  2. Convert that string into a list using Split().
  3. Use For loop to iterate over each word in the list.
  4. Using Indexing exchange the first and the last character of each word.
  5. Replace the new word formed with the old word in the list.
  6. After iterating over the last element convert the list into the string using join().
  7. Print the output
				
					#Input string
string ="Its Online Learning"

#Creating list
list1 = string.split(" ")

#Loop for swapping characters
for word in list1:
    #Creating new word
    new_word = word[-1]+word[1:-1]+word[0]
    #Finding index of the word
    index = list1.index(word)
    #Replacing the word with new word
    list1[index] = new_word
    
#Joining list and printing output    
" ".join(list1)
				
			

Output :

				
					'stI enlinO gearninL'
				
			

swap the last character of a given string.

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

Swap the last character of a string

In this program, we will take a string as input and swap the last character of a string

Steps to solve the program
  1. Take a string as input.
  2. Swap the last character of a string using Indexing.
  3. Print the output.
				
					#input string
string = "SqaTool"

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

Output :

				
					lqaTooS
				
			

replace the second occurrence of any char with the special character $.

exchange the first and last character of each word