# 1. Python function that takes a list of words and return the longest word and the length of the longest one.
words=input("Enter words or a sentence:")
st_word=[]
st_word=words.split()
lenght=len(st_word)
i=0
temp=""
long_word=""
index=0
while i < lenght:
if i==0 and lenght == 1:
#print("longest word is :",st_word[i],"Lenght is :",len(st_word[i]))
break
if i == lenght-1:
index=i
index-=1
else:
index=i
if len(st_word[index]) >= len(st_word[index+1]):
if len(long_word) < len(st_word[index]):
long_word=st_word[index]
#temp=st_word[i]
#else:
#long_word=st_word[i+1]
#temp=st_word[i+1]
else:
if len(long_word) < len(st_word[index]):
long_word=st_word[index]
i+=1
print("longest word is :",st_word[i],"Lenght is :",len(st_word[i]))
Enter words or a sentence:reva longest word is : reva Lenght is : 4
#2.Python function to remove the nth index character from a nonempty string.
word=input("Enter a string:")
n=int(input("Enter a index number:"))
new_word=word.replace(word[n],"")
print("New word after removing nth index charatacter:",new_word)
Enter a string:revathi Enter a index number:2 New word after removing nth index charatacter: reathi
# 3. . Python function to get the last part of a string before a specified character.
words=input("Enter words or a sentence:")
spec_char=input("Enter a specified character:")
st_word=[]
st_word=words.split(spec_char)
print(st_word[-1:])
Enter words or a sentence:reva/revs/is/a Enter a specified character:/ ['a']
#4. Python function to sort a string lexicographically.
def sort_fun(words):
st_word=[]
st_word=words.split()
st_word.sort()
for i in st_word:
print (i)
words=input("Enter words or a sentence:")
sort_fun(words)
Enter words or a sentence:reva is a good girl a girl good is reva
#5.Python function to remove spaces from a given string.
def rem_space(words):
words=words.strip(" ")
new_word=""
st_word=[]
st_word=words.split()
i=0
while i < len(st_word):
new_word=new_word+st_word[i]
i+=1
print(new_word)
words=input("Enter words or a sentence:")
rem_space(words)
Enter words or a sentence:reva is good revaisgood