Friday, 7 October 2022

Display the numbers and its positions

a=[4,7,3,2,5,9]

for i in a:

    print(str(i)+' is in the position '+ str(a.index(i)+1))


Output:

4 is in the position 1
7 is in the position 2
3 is in the position 3
2 is in the position 4
5 is in the position 5
9 is in the position 6

Split the sentence into words

my_str = "Welcome to Python"

# breakdown the string into a list of words

words = my_str.split()

# sort the list

words.sort()

# display the sorted words

print("The sorted words are:")

for word in words:

   print(word)


Output:

Sorted words are:
Python
Welcome
to

Find a string is Palindrome or not

my_str = "AbcdDCBa"

#for caseless comparison
my_str = my_str.casefold()

#Reverse a string
rev_str = reversed(my_str)

if list(my_str) == list(rev_str):
    print("It is a palindrome")
else:
        print("It is not a palindrome")