def find_longest_word(words_list):
word_len = []
for n in words_list:
word_len.append((len(n), n))
word_len.sort()
return word_len[-1][0], word_len[-1][1]
result = find_longest_word(["Python", "Exercises", "Assignment"])
print("\nLongest word: ",result[1])
print("Length of the longest word: ",result[0])
Longest word: Assignment Length of the longest word: 10
string = input("Enter the string: ")
n = 2
first_part = string[:n]
second_part = string[n+1:]
print("Modified string: ", first_part + second_part)
Enter the string: I am studying Python Programming Modified string: I m studying Python Programming
# Python function to get the last part of a string before a specified character.
string = "I am studying Python-Programming"
print(string.rsplit("-",1)[0])
I am studying Python
# Python function to sort a string lexicographically.
def lexicographically_sort(string):
return sorted (sorted(string))
string = "I am a student of Python programming"
print(lexicographically_sort("I am a student of Python programming"))
[' ', ' ', ' ', ' ', ' ', ' ', 'I', 'P', 'a', 'a', 'a', 'd', 'e', 'f', 'g', 'g', 'h', 'i', 'm', 'm', 'm', 'n', 'n', 'n', 'o', 'o', 'o', 'p', 'r', 'r', 's', 't', 't', 't', 'u', 'y']
string = " I am removing spaces from string by doing this. "
print("Original string:", string)
print ("Modified string:", str.strip(string))
Original string: I am removing spaces from string by doing this. Modified string: I am removing spaces from string by doing this.