Assignment #3 - Mohammed Abdul Wasay
# function to find the longest word and its length in the list
def longestword(word):
maxlen1 = len(word[0])
temp = word[0]
# for loop to traverse the list
for i in word:
if(len(i) > maxlen1):
maxlen1 = len(i)
temp = i
print("The longest word is:", temp,
" and its length is ", maxlen1)
list1 = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday","Saturday"]
longestword(list1)
The longest word is: Wednesday and its length is 9
def remove_nth_char(str,n):
# extracts 0 to n-1th index using slicing
str_before_nth = str[:n]
# extracts characters from n+1th index until the end
str_after_nth = str[n+1:]
print("String after removing the ", n, "th character is")
# combining both the parts together
print(str_before_nth + str_after_nth)
str1="yearning too learn python"
remove_nth_char(str1,11)
String after removing the 11 th character is yearning to learn python
def getlastpart(txt, gchar):
stringpart = txt.partition(gchar)
print(stringpart)
str3 = "docs.python.org"
getlastpart(str3,',')
('docs.python.org', '', '')
def get_lstpart_str(my_str, char):
partitioned_string = my_str.rpartition(char)
print(partitioned_string)
#before_last_char = partitioned_string[0]
#print(before_last_char)
a_string = "docs.python.org"
get_lstpart_str(a_string,'.')
('docs.python', '.', 'org')
b_str ="apple#banana#mango"
get_lstpart_str(b_str,'#')
('apple#banana', '#', 'mango')
txt = "apple#banana#cherry#orange"
# setting the maxsplit parameter to 1, will return a list with 2 elements!
x = txt.split("#", 1)
print(x)
['apple', 'banana#cherry#orange']
def sortingbyLexi(my_string):
# Split the my_string till where space is found.
words = my_string.split()
# sort() will sort the strings.
words.sort()
# Iterate i through 'words' to print the words
# in alphabetical manner.
for i in words:
print( i )
sortingbyLexi("banana grapes apples oranges peaches mango")
apples banana grapes mango oranges peaches
# function to remove spaces from a given string
import string
def remove(string):
return "".join(string.split())
string = "H e l l o P y t h o n"
remove(string)
'HelloPython'