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

convert half the string to uppercase.

In this program, we will take a string as input and convert half the string to uppercase.

Steps to solve the program
  1. Take a string as input.
  2. Convert the half of the string upper() and Indexing.
  3. Print the output.
				
					#Input string
str1 = "banana"

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

Output :

				
					banANA
				
			

insert space before every capital letter appears in a given word

split and join a string using “-“

Number of leap years within a range of years

In this program, we will take a range of leap years as input and find the number of leap years within a range of years.

Steps to solve the program
  1. Take a range of years as input.
  2. Split the range using split() so that will get a list of starting year and ending year.
  3. Create a variable and assign its value equal to 0.
  4. Use for loop to get a range of years between starting year and the ending year.
  5. During iteration check whether a year is a leap year to not.
  6. If it is a leap year then add 1 to the variable that we created at the start.
  7. Print the output.
				
					#Input string
str1 ="1981-2001"
l = str1.split("-")
total = 0

for i in range(int(l[0]),int(l[1])+1):
    if (i % 400 == 0 or i % 100 !=0) and i % 4 == 0:
        total += 1
        
#Printing output
print("Range of years:", str1)
print("Total leap year: ",total)
				
			

Output :

				
					Range of years: 1981-2001
Total leap year:  5
				
			

extract name from a given email address

insert space before every capital letter appears in a given word

Extract the name from the email address

In this program, we will take email as string input and extract the name from the email address.

Steps to solve the program
  1. Take an email id of a student as string input.
  2. Find the index number of “@” using index().
  3. Create an empty string.
  4. Add characters from the email id to the empty string using for loop up to the index of “@”.
  5. Add only alphabets not numbers use isalpha() for this purpose.
  6. Print the output.
				
					#Input string
str1 = "student1@gmail.com"
index = str1.index("@")
str2 = ""

for char in str1[:index]:
    if char.isalpha():
        str2 += char
        
#Printing output
print(str2)
				
			

Output :

				
					student
				
			

add two strings as they are numbers

count the number of leap years within the range of years

Add two strings as they are numbers

In this program, we will take two numbers as strings and add two strings as they are numbers.

Steps to solve the program
  1. Take two numbers as a string.
  2. Add two strings after converting them into integers using int().
  3. Print the output.
				
					#Printing output
num1 = "3"
num2 = "7"

result = int(num1)+int(num2)

#Printing output
print(result)
				
			

Output :

				
					10
				
			

split a given multiline string into a list of lines

extract name from a given email address

Split a given multiline string into list of lines

In this program, we will take a multiline string as input and split a given multiline string into list of lines.

Steps to solve the program
  1. Take a multiline string as input.
  2. Split the string on a new line using split() to convert the given string into a list of lines.
  3. Print the output.
				
					#Input string
str1= "This string Contains\nMultiple\nLines"

#Printing output
print(str1.split("\n"))
				
			

Output :

				
					['This string Contains', 'Multiple', 'Lines']
				
			

extract numbers from a given string

add two strings as they are numbers

Extract numbers from a given string

In this program, we will take a string as input and extract numbers from a given string.

Steps to solve the program
  1. Take a string as input.
  2. Create an empty list.
  3. Split the given list using split().
  4. Use for loop to iterate over splitter string.
  5. If the value is a number then add it to the empty list.
  6. Print the output.
				
					#Input string
str1 = "python 456 self learning 89"
list1 = []

for char in str1.split(" "):
    if char.isdigit():
        list1.append(int(char))

#Printing output        
print(list1)
				
			

Output :

				
					[456, 89]
				
			

find the string similarity between two given strings

split a given multiline string into a list of lines

Find the string similarity between two strings

In this program, we will take two strings as input and  find the string similarity between two given strings

Steps to solve te program
  1. Take 2 strings as inputs.
  2. Import difflib library.
  3. From difflib library use SequenceMatcher to find the similarity between two strings.
  4. Print the output.
				
					#Input string
str1 = "Learning is fun in Sqatools"
str2 = "Sqatools Online Learning Platform"

import difflib
result =  difflib.SequenceMatcher(a=str1.lower(), b=str2.lower())

#Printing output
print(result.ratio())
				
			

Output :

				
					0.4
				
			

remove unwanted characters from a given string

extract numbers from a given string