Practice question (22-07-2023)

  1. Write a Python program to take a string as input and return a new string where the first and last characters have been exchanged.

2.Create a Python function that takes a list of strings as input and returns the longest string in the list.

3.Write a Python program to check if a given string is a palindrome (a word, phrase, number, or other sequence of characters that reads the same forward and backward).

4.Create a Python function that takes a sentence as input and returns the number of words in the sentence.

5.Write a Python program to remove all the vowels from a given string.

2 Likes
1 Like
1 Like

`
#[1]–> write a pyhton program to take string as input and return a new string where the first and last characters have

been exchanged.

string = input(“Enter a string: “)
x = list(string)
temp = x[0]
x[0] = x[-1]
x[-1] = temp
print(””.join(x))

[2]–> create a python function that takes a list of strings as input and return the longest in the list

def longest(n):
longest_string=n[0]
for i in n:
if len(i)>len(longest_string):
longest_string=i
return longest_string
n=[“bike”, “train”, “bicycle”, “aeroplane”,]
print(‘Longest String is:’,longest(n))

[3]–> Write a Python program to check if a given string is a palindrome

(a word, phrase, number, or other sequence of characters that reads the same forward and backward).

def Palindrome(string):
if string == string[::-1]:
return “Palindrome”
else:
return “Not Palindrome”

string = input("Enter a string: ")
print(Palindrome(string))

[4]–> Create a Python function that takes a sentence as input and returns the number of words in the sentence.

def words(str):
a = str.split()
return len(a)

string = input("Enter the senteces: ")
res = words(string)
print(res)

[5]–> Write a Python program to remove all the vowels from a given string.

string = input(" Enter a string: “)
vowels = “aeiou”
l = []
for i in list(string):
if i in list(vowels):
continue
else:
l.append(i)
print(”".join(l))

1 Like

s=input(“enter the string”)
d=list(s)
temp=d[0]
d[0]=d[-1]
d[-1]=temp
s=‘’.join(d)
print(s)

1 Like

def words(r):
r=s.split()
return(len(r))

words(“the boy is very strong”)

3
s=input()
if s==s[::-1]:
print(“the string is palindrome”)
else:
print(“the string is not palindrome”)

1 Like

l=input()
v={‘a’,‘e’,‘i’,‘o’,‘u’}
w=[]
for i in l:
if i not in v:
w.append(i)
print(w)
print(“”.join(w))

3
s=input()
if s==s[::-1]:
print(“the string is palindrome”)
else:
print(“the string is not palindrome”)

1 Like

List =[‘krishnan’,‘work’,‘join’,‘went’,‘coming’]
max_len = -1
for i in List:
if len(i) > max_len:
max_len = len(i)
result= i
print(“Maximum length of the string among the list is:”,result)

  1. Write a Python program to take a string as input and return a new string where the first and last characters have been exchanged.

word=str(input(“Enter the string:”))
def swap(word):
len1 = word[0]
len2 = word[-1]
swapword = len2 + word[1:-1] + len1
print(swapword)
swap(word)
.Create a Python function that takes a sentence as input and returns the number of words in the sentence.

string=str(input(“Enter the sentence:”))
def count_word(string):
pass
count1=len((string.split()))
print(“The count of the sentence is:”,count1)
count_word(string)
5.Write a Python program to remove all the vowels from a given string.
word = “swarnakumari”
vowels = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’, ‘A’, ‘E’, ‘I’, ‘O’, ‘U’]
new_string = 0
for i in word:
if i in vowels:
word=word[:new_string]+word[new_string+1:]
new_string=new_string-1
new_string = new_string+1
print("After removing Vowels: ", word)

1 Like

Write a Python program to take a string as input and return a new string where the first and last characters have been exchanged.

string = input("Enter the string: ")

if len(string) > 0:
mod_str = string[5]+string[1:5]+string[0]

print("Taken from user: ",string)
print("Modified string: ",mod_str)

else:
print(“Check ur Logic”)