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

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