Floating numbers up to 3 decimal places.

In this program, we will take a number as input and print floating numbers up to 3 decimal places, and convert the number to a string.

Steps to solve the program
  1. Take a floating number as input.
  2. Create an empty string.
  3. Use the round() function to round up the given floating number up to 3 decimal places.
  4. Conver rounded number to a string using str() and add it to the empty string.
  5. Print thee output.
				
					#Input string
num = 2.14652
result = ""

#Rounding number
result += str(round(num, 3))

#Printing output
print(result)
				
			

Output :

				
					2.147
				
			

split and join a string

convert numeric words to numbers.

Split and join a string

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

Steps to solve the program
  1. Take a string as input.
  2. Split the given string using Split().
  3. Join the splitter string using join() and ““.
  4. Print the output.
				
					#input string
string = "Hello world"

#Splitting the string
new_str = string.split(" ")
print(new_str)

#Joinin using -
print("-".join(new_str))
				
			

Output :

				
					['Hello', 'world']
Hello-world
				
			

remove a new line from a string

print floating numbers up to 3 decimal places and convert it to string.

Remove a new line from a string

In this program, we will take a string as input and remove a new line from a string.

Steps to solve the program
  1. Take a string as input.
  2. Remove the new line from the string using strip().
  3. Print the output.
				
					#Input string
string1= "objectorientedprogramming\n"

#Printing output
print(string1.rstrip())
				
			

Output :

				
					objectorientedprogramming
				
			

convert all the characters in a string to Upper Case.

split and join a string

Convert all characters to Uppercase in a string.

In this program, we will take a string as input and convert all characters to Uppercase in a string

Steps to solve the program
  1. Take a string as input.
  2. Convert all characters of the given string to Uppercase using Upper().
  3. Print the output.
				
					#Input string
string1 = "I live in pune"

#Printing output
print(string1.upper())
				
			

Output :

				
					I LIVE IN PUNE
				
			

exchange the first and last letters of the string

remove a new line from a string in a python.

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.