Exchange first & last letters of the string

In this program, we will take a string as input and exchange first & last letters of the string

Steps to solve the program
  1. Take a string as input.
  2. Exchange the first and last letters of the string using Indexing.
  3. Print the output.
				
					#Input string
string1= "We are learning python"

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

Output :

				
					ne are learning pythoW
				
			

check if a string has a special character or not

convert all the characters in a string to Upper Case.

Check if a string has special character or not

In this program, we will take a string as input and check if a string has special character or not.

Steps to solve the program
  1. Take a string as input.
  2. Create a string with all special characters as its value.
  3. Create a count variable and assign its value equal to zero.
  4. Use For loop to iterate over each character of the string.
  5. If the character is in a string of special characters then add 1 to the count variable.
  6. In the end, if the count variable is greater than 0 then the string has a special character.
  7. Print the output.
				
					#Input strings
string = "python$$#sqatools"

#Special characters
s ='[@_!#$%^&*()<>?/\|}{~:]'
count = 0

for char in string:
    if char in s:
        count += 1
        
#Printing output
if count > 0:
    print("Given string has special characters")
else:
    print("‘Given string does not has special characters")
				
			

Output :

				
					Given string has special characters
				
			

remove all duplicate characters from a given string

exchange the first and last letters of the string

Remove all duplicate characters from a string

In this program, we will take a string as input and remove all duplicate characters from a string

Steps to solve the program
  1. Take a string as input.
  2. Import OrderedDict from collections to remove duplicate characters from a string.
  3. Print the output.
				
					#Input string
string = "sqatools"

#Importing ordereddict
from collections import OrderedDict

#Printing output
"".join(OrderedDict.fromkeys(string))
				
			

Output :

				
					'sqatol'
				
			

print characters at odd places in a string.

check if a string has a special character or not

Print characters at odd places in a string.

In this program, we will take a string as input and print characters at odd places in a string.

Steps to solve the program
  1. Take a string as input.
  2. Use the range function to find the odd places from the length of the string.
  3. Print only those characters at odd places in a string using Indexing.
				
					#Input string
string = "abcdefg"

for i in range(len(string)):
    if i%2 != 0:
    #Printing output
        print(string[i],end="")
				
			

Output :

				
					bdf
				
			

count the number of consonants in a string.

remove all duplicate characters from a given string

Count the number of consonants in a string.

In this program, we will take a string as input and count the number of consonants in a string.

Steps to solve the program
  1. Take a string as input.
  2. Create a list containing all the vowels and a count variable and assign its value equal to zero.
  3. Use For loop to iterate over each character of the string.
  4. If the character is not in the vowels list add 1 to the count variable.
  5. Print the output.
				
					#Input string
string = "sqltools"
vowel = ["a","e","i","o","u","A","E","I","O","U"]
count = 0

for char in string:
    if char not in vowel:
        count += 1

#Printing output
print("Number of consonants: ",count)
				
			

Output :

				
					Number of consonants:  6
				
			

count the number of vowels in a string.

print characters at odd places in a string.

Count the number of vowels in a string

In this program, we will take a string as input and count the number of vowels in a string.

Steps to solve the program
  1. Take a string as input.
  2. Create a list containing all the vowels and a count variable and assign its value equal to zero.
  3. Use For loop to iterate over each character of the string.
  4. If the character is a vowel add 1 to the count variable.
  5. Print the output.
				
					#Input string
string = "I am learning python"
vowel = "aeiou"
count=0

for char in string:
    if char.lower() in vowel:
        count += 1
        
#Printing output
print("Number of vowels: ",count)
				
			

Output :

				
					Number of vowels:  6
				
			

check if a string has a number or not.

count the number of consonants in a string.

Check if a string has a number or not

In this program, we will take a string as input and check if a string has a number or not.

Steps to solve the program
  1. Take a string as input.
  2. Create a count variable and assign its value equal to 0.
  3. Use for loop to iterate over each character of the string.
  4. If the character is a number add 1 to the count variable.
  5. Use isnumeric() to check for numbers.
  6. If in the end count is greater is 0 then the string has a number.
  7. Print the output. 
				
					#Input string
string = "python1"
count = 0

for char in string:
    if char.isnumeric():
        count += 1
        
#Checking for numbers
if count > 0:
    print("Given string have a number")
else:
    print("Given string does not have a number")
				
			

Output :

				
					Given string have a number
				
			

print characters at even places in a string.

count the number of vowels in a string.

Print characters at even places in a string.

In this program, we will take a string as input and print characters at even places in a string.

Steps to solve the program
  1. Take a string as input.
  2. Use the range function to find the even places from the length of the string.
  3. Print only those characters at even places in a string using Indexing.
				
					#Input string
string ="sqatools"

for i in range(len(string)):
    if i%2 == 0:
    #Printing output
        print(string[i], end = "")
				
			

Output :

				
					saol
				
			

combine two strings into one.

check if a string has a number or not.

Combine two strings into one.

In this program, we will take two strings as input and combine two strings into one.

Steps to solve the program
  1. Take two strings as inputs.
  2. Combine them using “+” operator.
  3. Print the output.
				
					#Input string
string1 = "abc"
string2 = "def"

#Printing output
print(string1 + string2)
				
			

Output :

				
					abcdef
				
			

calculate the frequency of each character in a string.

print characters at even places in a string.

Calculate frequency of each character in string

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

Steps to solve the program
  1. Take a string as input and create an empty dictionary.
  2. Use For loop to iterate over each character in the string.
  3. Add the character as a key in the dictionary and its count in the string as its value.
  4. Use count() to count how many times a character has appeared in the string.
  5. Print the output.
				
					#Input string
string = "sqatools"
dictionary = dict()

for char in string:
    dictionary[char] = string.count(char)

#Printing output    
print(dictionary)
				
			

Output :

				
					{'s': 2, 'q': 1, 'a': 1, 't': 1, 'o': 2, 'l': 1}
				
			

calculate the length of a string.

combine two strings into one.