Logical Practice Questions - ( DS140823 ) - 15-09-2023

  1. Write a Python function to reverses a string if it’s length is a multiple of 4

  2. Write a Python program to check whether a string contains all letters of the alphabet

  3. Write a Python program to remove spaces from a given string

2 Likes
# 1. Write a Python function to reverses a string if it’s length is a multiple of 4

def reverse(string):
    string = string[::-1]
    return string
 
user_input = str(input("Enter a string: "))
print("The original string is : ", user_input)

if len(user_input) % 4 == 0:
    print("The reversed string is : ", reverse(user_input))
else:
    print("Size not a multiple of 4!")


# 2. Write a Python program to check whether a string contains all letters of the alphabet

user_input = str(input("Enter a string: "))
print("The original string is : ", user_input)
alphabet = "abcdefghijklmnopqrstuvwxyz"
flag = True
for char in alphabet:
    if char not in user_input.lower():
        flag = False       

if flag == False:
    print("All the alphabet Not present")
else: 
    print("All the alphabet present")



# 3. Write a Python program to remove spaces from a given

user_input = str(input("Enter a string: "))
print("The original string is : ", user_input)
user_output = user_input.replace(" ","")
print("Output: ",user_output)

1 Like
def reverse_string_if_multiple_of_4(input_string):
    if len(input_string) % 4 == 0:
        return input_string[::-1]
    else:
        return input_string

input_str = input("Enter a string: ")
result_str = reverse_string_if_multiple_of_4(input_str)
print("Result:", result_str)

import string

def contains_all_alphabet_letters(input_string):
    input_string = input_string.lower()
    alphabet_set = set(string.ascii_lowercase)
    input_set = set(input_string)
    return alphabet_set.issubset(input_set)
input_str = input("Enter a string: ")
if contains_all_alphabet_letters(input_str):
    print("The string contains all letters of the alphabet.")
else:
    print("The string does not contain all letters of the alphabet.")
def remove_spaces(input_string):
    new_strlst=input_string.split(" ")
    new_str=''
    for i in new_strlst:
        new_str+=i
    print(new_str)
string=input('Enter the string value ')
remove_spaces(string )

1 Like
1)
word =input("Enter the value: ")
if len(word)%4==0:
    rev =word[::-1]
    print(rev)
else:
    print("Length of word is not the multiple of 4")  

2)

alphabet=str(["a","b","c","d","e","f","g","h","i","j","K","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]) 
instr =input("Enter the string: ").lower()
for i in instr:
    if i not in alphabet:
        print("All the alphabets are not in the ")
        break
else:
    print("All the alphabets are in the list")  

3)

word = input("Enter the string value: ")
remsp= word.replace(" ","") 
print(remsp)

1 Like
1.
def reverse_string_if_multiple_of_4(input_str):
    if len(input_str) % 4 == 0:
        return input_str[::-1]
    else:
        return input_str

value = input("Enter a string: ")
result = reverse_string_if_multiple_of_4(value)
print("Reversed string (if length is a multiple of 4):", result)

2.
def contains_all_alphabet_letters(input_str):
    input_set = set(input_str.lower())
    alphabet_set = set(string.ascii_lowercase)
    
    return alphabet_set <= input_set

value = input("Enter a string: ")
result = contains_all_alphabet_letters(value)
if result:
    print("The string contains all letters of the alphabet.")
else:
    print("The string does not contain all letters of the alphabet.")

3.
def remove_spaces(input_str):
    return input_str.replace(" ", "")

value = input("Enter a string: ")
result = remove_spaces(value)
print("String with spaces removed:", result)

1 Like
# 1st question

def reverse(str):
    str=str[::-1]
    return str

str1=input("enter your string:")
if len(str1)%4==0:
    print(reverse(str1))
else :
    print("string is not multiple of 4")


# 2nd question

def alpha(str):
    str2=set("abcdefghijklmnopqrstuvwxyz")
    str=set(str)
    if str2.issubset(str):
        return True
    return False

str=input("enter your string:")
if alpha(str):
    print("string contain all letters of alphabets")
else:
    print("string donot contain all letters of aphabets")

# 3rd question

def remove(str,old):
    str1=str.split(old)
    return "".join(str1)

str=input("enter your string:")
if " " in str:
     str=remove(str," ")
print("string after removing spaces:",str)
1 Like
# 1} Write a Python function to reverses a string if it’s length is a multiple of 4

string_input = str(input("Enter your string: "))

if len(string_input) % 4 == 0 :
    print(string_input[::-1])
else:
    print("The given string's length is not a multiple of 4")

# 2} Write a Python program to check whether a string contains all letters of the alphabet

str1 = str(input("Enter a string value : "))
lower_case = "abcdefghijklmnopqrstuvwxyz"
upper_case = "ABCDEFGIJKLMNOPQRSTUVWXYZ"

total = lower_case + upper_case
num = 0
for i in str1:
    if i in total:
        num += 1
if num == len(str1):
    print("String contains only letters")
else:
    print("String doesn't contains only letters")

# 3} Write a Python program to remove spaces from a given string.

string = input("Enter a String : ")
result = ''

for i in string:
    if i != ' ':
        result += i
print("String after removing the spaces : ",result)
1 Like

Ans 1 -

def reverse_string_if_multiple_of_4(input_string):
    if len(input_string) % 4 == 0:
        return input_string[::-1]
    else:
        return input_string   
input_string = input("Enter a string: ")
result_string = reverse_string_if_multiple_of_4(input_string)
print("Result:",result_string)

Ans 3 -

user_input = str(input("Enter a string: "))
print("The original string is : ", user_input)
user_output = user_input.replace(" ","")
print("Output: ",user_output)
1 Like
# 1. Write a Python function to reverses a string if it’s length is a multiple of 4
string=input("enter string")
print("given string is: ",string)
def reverse_string(string):
    l=len(string)
    if l%4==0:
       string_rev=string[::-1]
       return('reverse of string is:", string_rev)
    else:
       return "reverse of string is not possible"
reverse_string(string)

output:-
enter stringsmamatha
given string is:  smamatha
('reverse of string is:', 'ahtamams')
#Write a Python program to remove spaces from a given string

def remove(string):

return "".join(string.split())

string = input("enter string:")

x=[c for c in string]

print(x)

print(remove(string))
output:-
enter string:mamatha
['m', 'a', 'm', 'a', 't', 'h', 'a']
mamatha
# que1.  Write a Python function to reverses a string if it’s length is a multiple of 4

char =input("Enter char: ")
if len(char)%4==0:
    rev =char[::-1]
    print(rev)
else:
    print("Length of char is not the multiple of 4") 


# que2. Write a Python program to check whether a string contains all letters of the alphabet

str = input("Enter a string: ")
print("The original string is : ", str)
alphabets = "abcdefghijklmnopqrstuvwxyz"
flag = True
for char in alphabet:
    if char not in str.lower():
        flag = False       

if flag == False:
    print("alphabets absent")
else: 
    print("alphabets present")


# que3. Write a Python program to remove spaces from a given string

def remove_spaces(input_str):
    return input_str.replace(" ", "")

value = input("Enter a string: ")
result = remove_spaces(value)
print("String with spaces removed:", result)