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

Add ‘ing’ at the end of the string.

In this program, we will take a string as input and add ‘ing’ at the end of the string.

Steps to solve the program
  1. Take a string as input.
  2. Add ing at the end of the string using “+“.
  3. Print the output.
				
					#Input string
str1 = "xyz"

#Input string
print(str1+"ing")
				
			

Output :

				
					xyzing
				
			

check if a given string is binary or not.

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

Check if a given string is binary or not.

In this program, we will take a string as input and check if a given string is binary or not.

Steps to solve the program
  1. Take a string as input.
  2. Create a variable count and assign its value equal to 0.
  3. Use for loop to iterate over each character in the string.
  4. If the character is 0 or 1 then add 1 to the count variable.
  5. If the value of the count variable is equal to the length of the string then string is binary.
  6. Print the output.
				
					#Input string
str1 = "01011100"
count = 0

for char in str1:
    if char == "0" or char == "1":
        count += 1

#Printing output
if count == len(str1):
    print("Yes")
else:
    print("No")
    
    
str1 = "sqatools 100"
count = 0

for char in str1:
    if char == "0" or char == "1":
        count += 1
        
if count == len(str1):
    print("Yes")
else:
    print("No")
				
			

Output :

				
					Yes

No
				
			

remove the kth element from the string

add ‘ing’ at the end of the string

Remove the kth element from the string

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

Steps to solve the program
  1. Take a string as input.
  2. Print the string without kth element using Indexing.
				
					#Input string
str1 = "sqatools"

#Printing output
print(str1[:2]+str1[3:])
				
			

Output :

				
					sqtools
				
			

accept a string that contains only vowels

check if a given string is binary or not.

Accept a string containing vowels only.

In this program, we will accept a string containing vowels only. If it has any consonants do not accept it.

Steps to solve the program
  1. Take a string as input.
  2. Check whether the string contains any consonants.
  3. If yes then, print Not Accepted else print Accepted.
  4. Use for loop for this purpose.
				
					#Input string
str1 = "python"
count = 0
List = ["a","e","i","o","u","A",
       "E","I","o","U"]

for char in str1:
    if char in List:
        count += 1

if count == len(str1):
    print("Accepted")
else:
    print("Not Accepted")
    
    
str2 = "aaieou"
count = 0
List = ["a","e","i","o","u","A",
       "E","I","o","U"]

for char in str2:
    if char in List:
        count += 1

if count == len(str2):
    print("Accepted")
else:
    print("Not Accepted")
				
			

Output :

				
					Not Accepted

Accepted
				
			

avoid spaces in string and get the total length

remove the kth element from the string

Avoid spaces in string and get the total length

In this program, we will take a string as input and avoid spaces in the string and get the total length.

Steps to solve the program
  1. Take a string as input.
  2. Use for loop to iterate over each character of the string.
  3. Use isspace() to check whether a character is a space or not.
  4. Find the total of such characters who are not space.
  5. Print the output.
				
					#Input string
str1 = "sqatools is best for learning python"
result = sum(not char.isspace() for char in str1)

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

Output :

				
					Length : 31
				
			

find permutations of a given string

accept a string that contains only vowels

Program to find permutations of a string

In this program, we will take a string as input and find permutations of a string using the function.

Steps to solve the program
  1. Take a string as input.
  2. From itertools import permutations to find the permutations of a string.
  3. Print the output.
				
					#Input string
str1 = "CDE"

from itertools import permutations
List = permutations(str1)

for combinations in list(List):
    print (''.join(combinations))
				
			

Output :

				
					CDE
CED
DCE
DEC
ECD
EDC
				
			

split and join a string using “-“

avoid spaces in string and get the total length

Split and join a string using “-“

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

Steps to solve the program
  1. Take a string as input.
  2. Split the string using split().
  3. Join the splitter string using join().
  4. Print the ouput.
				
					#Input string
str1 = "Sqatools is best"
list1 = str1.split(" ")

#Printing output
print("-".join(list1))
				
			

Output :

				
					Sqatools-is-best
				
			

uppercase half string

find permutations of a given string

Insert space before every capital letter

In this program, we will take a string as input and insert space before every capital letter appears in a given word.

Steps to solve the program
  1. Take a string as input and create an empty string.
  2. Use for loop to iterate over every character of the string.
  3. Add each character to the empty string.
  4. If a character is Capital letter then add space first then the character.
  5. Print the output.
				
					#Input string
str1 = "SqaTools pyThon"
result = ""

for char in str1:
    if char.isupper():
        result=result+" "+char
    else:
        result=result+char

#Printing output        
print(result)
				
			

Output :

				
					Sqa Tools py Thon
				
			

count the number of leap years within the range of years

uppercase half string