Get a list of mobile numbers from the string

In this program, we will take a string as input and get a list of mobile numbers from the given string.

Steps to solve the program
  1. Take a list 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 list.
  4. If the length of the word is 10 and that word contains all numbers then add that word to the empty list.
  5. Print the output.
				
					#Input string
str1 = """"We have 2233 some employee 8988858683 whos 3455 mobile numbers 
        are randomly distributed 2312245566 we want 453452 to get 4532892234 
        all the mobile numbers 9999234355  from this given string"""
List1 = str1.split(" ")
List2 = []

for val in List1:
    if len(val) == 10 and val.isnumeric():
        List2.append(val)

        
#Printing outptut
print(List2)
				
			

Output :

				
					['8988858683', '2312245566', '4532892234', '9999234355']
				
			

get all the email id’s from given string

Get all the email id’s from the given string

In this program, we will take a string as input and get all the email id’s from the given string.

Steps to solve program
  1. Take a string as input.
  2. Convert the string into a list using split() and create an empty list.
  3. Using for loop iterate over each word in the string.
  4. If the character in the word contains “@” add that word to the empty string.
  5. Print the output.
				
					#Input string
str1 = """We have some employee whos john@gmail.com email id’s are 
        randomly distributed jay@lic.com we want to get hari@facebook.com 
        all the email mery@hotmail.com id’s from this given string"""
List = str1.split(" ")
List1 = []

for char in List:
    for val in char:
        if val == "@":
            List1.append(char)
            
#Printing output
print(List1)
				
			

Output :

				
					['john@gmail.com', 'jay@lic.com', 'hari@facebook.com', 'mery@hotmail.com']
				
			

print each character on a new line

get a list of all the mobile numbers from the given string

Remove repeated characters in a string

In this program, we will take a string as input and remove repeated characters in a word of a string and replace it with a single letter.

Steps to solve the program
  1. Take a string as input.
  2. Remove the repeated characters in a string using set().
  3. Print the output.
				
					#Input string
str1 = "aabbccdd"

#Printing output
print("".join(set(str1)))
				
			

Output :

				
					cabd
				
			

swap cases of a given string

print a string 3 times

Program to swap cases of a given string

In this program, we will take a string as input and swap cases of a given string.

Steps to solve the program
  1. Take a string as input.
  2. Swap the cases of the characters using swapcase().
  3. Print the output.
				
					#Input string
str1 = "Learning Python"

#Printing output
print(str1.swapcase())
				
			

Output :

				
					lEARNING pYTHON
				
			

find the first repeated character in a string and its index.

remove repeated characters in a string and replace it with a single letter

Find the first repeated character and its index.

In this program, we will take a string as input and find the first repeated character in a string and its index.

Steps to solve the program
  1. Take a string as input.
  2. Use for loop with range() function to find the first repeated character.
  3. Find the index of that character using Index().
  4. After finding the first character break the loop.
  5. Print the output.
				
					#Input string
str1 = "sqatools"

for i in range(len(str1)):
    for j in range(i+1,len(str1)):
        if str1[i] == str1[j]:
            print(f"({str1[i]},{str1.index(str1[i])})")
    break
				
			

Output :

				
					(s,0)
				
			

index of each character in a string.

swap cases of a given string using python.

Print the index of each character in a string.

In this program, we will take a string as input and print the index of each character in a string.

Steps to solve the program
  1. Take a string as input.
  2. Use for loop to iterate over each character of the string.
  3. Find the index of the character using Index().
  4. Print the output.
				
					#Input string
str1 = "python"

for char in str1:
    print(f"Index of {char} is {str1.index(char)}")
				
			

Output :

				
					Index of p is 0
Index of y is 1
Index of t is 2
Index of h is 3
Index of o is 4
Index of n is 5
				
			

reverse words in a string using python.

find the first repeated character in a string and its index.

The reverse words in a string

In this program, we will take a string as input and reverse words in a string

Steps to solve the program
  1. Take a string as input.
  2. First split the string using split() and then reverse the words using reversed().
  3. Print the output.
				
					#Input string
str1 = "string problems"

#Input strinh
print(" ".join(reversed(str1.split(" "))))
				
			

Output :

				
					problems string
				
			

add ly at the end of the string if the given string ends with ing.

print the index of each character in a string.

Add ly at the end if the string ends with ing.

In this program, we will take a string as input and add ly at the end if the string ends with ing.

  1. Take a string as input.
  2. Check whether the string ends with ‘ing‘.
  3. If yes, then add ‘ly‘ at the end.
  4. Print the output.
				
					#Input string
str1 = "winning"

#Printing output
if str1[-3:] == "ing":
    print(str1+"ly")
				
			

Output :

				
					winningly
				
			

 add ‘ing’ at the end of the string

reverse words in a string